Table of Contents

Class CssCalc

Namespace
Egami.Css
Assembly
Egami.Css.dll

A length, percentage or number that may be a math function: calc() with + - * / and parentheses, and min(), max() and clamp() (CSS Values 4), nested freely. A plain length or number parses too, so a consumer can handle every <length-percentage> the same way. Resolve(Func<CssUnit, double>) turns it into pixels once the consumer knows what each unit is worth.

public sealed class CssCalc
Inheritance
CssCalc
Inherited Members

Examples

bool isLength = CssLength.TryParse("1.5em", out CssLength length);       // (1.5, CssUnit.Em)
new CssLength(1, CssUnit.In).TryToPixels(out double px);  // 96
bool isAngle = CssAngle.TryParse("0.25turn", out CssAngle angle);        // 90 degrees
bool isUrl = CssUrl.TryParse("url('pattern.svg#dots')", out CssUrl url);

// calc(), min(), max() and clamp(): the consumer says what each unit is worth.
bool isCalc = CssCalc.TryParse("calc(100% - 2em)", out CssCalc? calc);
double width = calc!.Resolve(unit => unit switch
{
    CssUnit.Percent => 3,   // a percent of a 300px box
    CssUnit.Em => 16,
    CssUnit.Px => 1,
    _ => double.NaN,
});                                                       // 268

Properties

HasPercentage

Gets whether any term is a percentage, which the consumer must resolve against a reference length.

public bool HasPercentage { get; }

Property Value

bool

IsNumber

Gets whether the value is a plain number (all its terms are unitless), such as calc(2 * 3).

public bool IsNumber { get; }

Property Value

bool

Methods

Resolve(Func<CssUnit, double>)

Computes the value.

public double Resolve(Func<CssUnit, double> pixelsPerUnit)

Parameters

pixelsPerUnit Func<CssUnit, double>

Gives the pixels one unit is worth, such as the font size for Em or a hundredth of the reference length for Percent; None is not asked for. Return NaN for a unit that cannot be resolved.

Returns

double

The value in pixels (or the number, for IsNumber). NaN becomes 0 and infinities are clamped.

ToString()

Returns the value as CSS text.

public override string ToString()

Returns

string

The text.

TryParse(CssComponentValue, out CssCalc?)

Parses one component value as a length, percentage, number or math function.

public static bool TryParse(CssComponentValue value, out CssCalc? calc)

Parameters

value CssComponentValue

The component value.

calc CssCalc

The value.

Returns

bool

True when the value is valid.

TryParse(IReadOnlyList<CssComponentValue>, out CssCalc?)

Parses component values as a length, percentage, number or math function.

public static bool TryParse(IReadOnlyList<CssComponentValue> values, out CssCalc? calc)

Parameters

values IReadOnlyList<CssComponentValue>

The component values (whitespace around the value is ignored).

calc CssCalc

The value.

Returns

bool

True when the values are one valid value.

TryParse(string, out CssCalc?)

Parses a length, percentage, number or math function.

public static bool TryParse(string text, out CssCalc? calc)

Parameters

text string

The CSS text, such as calc(100% - 2em).

calc CssCalc

The value.

Returns

bool

True when the text is valid and its terms have consistent types.