diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-01-21 17:43:34 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-01-21 19:05:57 +0100 |
| commit | 68add5b33e5d9f101306aaf84c373dc695b9ab81 (patch) | |
| tree | 5602c083a7b4708236e3fe70591e78fd05eb2d99 | |
| parent | c3461ec8d2a2972cf7ef93e29c756e609af6f94d (diff) | |
| download | chawan-68add5b33e5d9f101306aaf84c373dc695b9ab81.tar.gz | |
cssvalues: support percentage rgb colors, hsl & hsla
hsl is quite popular these days.
| -rw-r--r-- | src/css/cssparser.nim | 2 | ||||
| -rw-r--r-- | src/css/cssvalues.nim | 144 | ||||
| -rw-r--r-- | src/types/color.nim | 24 | ||||
| -rw-r--r-- | test/layout/color-functions.color.expected | 12 | ||||
| -rw-r--r-- | test/layout/color-functions.html | 16 |
5 files changed, 154 insertions, 44 deletions
diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim index 29ef455c..e60c340a 100644 --- a/src/css/cssparser.nim +++ b/src/css/cssparser.nim @@ -65,6 +65,8 @@ type cftUrl = "url" cftSrc = "src" cftVar = "var" + cftHsl = "hsl" + cftHsla = "hsla" CSSFunction* = ref object of CSSComponentValue name*: CSSFunctionType diff --git a/src/css/cssvalues.nim b/src/css/cssvalues.nim index 86309591..95a741c5 100644 --- a/src/css/cssvalues.nim +++ b/src/css/cssvalues.nim @@ -1,5 +1,6 @@ import std/algorithm import std/macros +import std/math import std/options import std/strutils import std/tables @@ -976,46 +977,61 @@ func getToken(cvals: openArray[CSSComponentValue]; i: int): Opt[CSSToken] = return ok(CSSToken(cval)) return err() -proc parseARGB(value: openArray[CSSComponentValue]): Opt[CSSColor] = - var commaMode = false +func getToken(cvals: openArray[CSSComponentValue]; i: int; + tt: set[CSSTokenType]): Opt[CSSToken] = + let tok = ?cvals.getToken(i) + if tok.t in tt: + return ok(tok) + return err() + +func getToken(cvals: openArray[CSSComponentValue]; i: int; t: CSSTokenType): + Opt[CSSToken] = + let tok = ?cvals.getToken(i) + if t == tok.t: + return ok(tok) + return err() + +func getColorToken(cvals: openArray[CSSComponentValue]; i: int; + legacy = false): Opt[CSSToken] = + let tok = ?cvals.getToken(i) + if tok.t in {cttNumber, cttINumber, cttDimension, cttIDimension, + cttPercentage}: + return ok(tok) + if not legacy and tok.t == cttIdent and tok.value == "none": + return ok(tok) + return err() + +# For rgb(), rgba(), hsl(), hsla(). +proc parseLegacyColorFun(value: openArray[CSSComponentValue]): + Opt[tuple[v1, v2, v3: CSSToken; a: uint8; legacy: bool]] = var i = value.skipBlanks(0) - template check_err(slash: bool) = - #TODO calc, percentages, etc (cssnumber function or something) - if not slash and i >= value.len: - return err() - if i < value.len: - let x = value[i] - if not (x of CSSToken and CSSToken(x).t in {cttNumber, cttINumber}): - return err() - template next_value(first = false, slash = false) = + let v1 = ?value.getColorToken(i) + i = value.skipBlanks(i + 1) + let legacy = ?value.getToken(i) == cttComma + if legacy: + if v1.t == cttIdent: + return err() # legacy doesn't accept "none" + inc i + i = value.skipBlanks(i) + let v2 = ?value.getColorToken(i, legacy) + if legacy: i = value.skipBlanks(i + 1) - if i < value.len: - if value[i] == cttComma and (commaMode or first): - # legacy compatibility - i = value.skipBlanks(i + 1) - commaMode = true - elif commaMode: - return err() - elif slash: - let tok = value[i] - if tok != cttDelim or CSSToken(tok).cvalue != '/': - return err() - i = value.skipBlanks(i + 1) - check_err slash - check_err false - let r = clamp(CSSToken(value[i]).nvalue, 0, 255) - next_value true - let g = clamp(CSSToken(value[i]).nvalue, 0, 255) - next_value - let b = clamp(CSSToken(value[i]).nvalue, 0, 255) - next_value false, true - let a = if i < value.len: - clamp(CSSToken(value[i]).nvalue, 0, 1) + discard ?value.getToken(i, cttComma) + i = value.skipBlanks(i + 1) + let v3 = ?value.getColorToken(i, legacy) + i = value.skipBlanks(i + 1) + if i == value.len: + return ok((v1, v2, v3, 255u8, legacy)) + if legacy: + discard ?value.getToken(i, cttComma) else: - 1 + if (?value.getToken(i, cttDelim)).cvalue != '/': + return err() + i = value.skipBlanks(i + 1) + let v4 = ?value.getToken(i, {cttPercentage, cttNumber, cttINumber}) if value.skipBlanks(i + 1) < value.len: return err() - return ok(rgba(int(r), int(g), int(b), int(a * 255)).cssColor()) + return ok((v1, v2, v3, uint8(clamp(v4.nvalue, 0, 1) * 255), legacy)) # syntax: -cha-ansi( number | ident ) # where number is an ANSI color (0..255) @@ -1056,6 +1072,42 @@ func parseANSI(value: openArray[CSSComponentValue]): Opt[CSSColor] = return ok(ANSIColor(i).cssColor()) return err() +proc parseRGBComponent(tok: CSSToken): uint8 = + if tok.t == cttIdent: # none + return 0u8 + var res = tok.nvalue + if tok.t == cttPercentage: + res *= 2.55 + return uint8(clamp(res, 0, 255)) # number + +type CSSAngleType = enum + catDeg = "deg" + catGrad = "grad" + catRad = "rad" + catTurn = "turn" + +# The return value is in degrees. +proc parseAngle(tok: CSSToken): Opt[float32] = + if tok.t in {cttDimension, cttIDimension}: + case ?parseEnumNoCase[CSSAngleType](tok.unit) + of catDeg: return ok(tok.nvalue) + of catGrad: return ok(tok.nvalue * 0.9f32) + of catRad: return ok(radToDeg(tok.nvalue)) + of catTurn: return ok(tok.nvalue * 360f32) + return err() + +proc parseHue(tok: CSSToken): Opt[float32] = + if tok.t in {cttNumber, cttINumber}: + return ok(tok.nvalue) + if tok.t == cttIdent: # none + return ok(0) + return parseAngle(tok) + +proc parseSatOrLight(tok: CSSToken): Opt[float32] = + if tok.t in {cttNumber, cttINumber, cttPercentage}: + return ok(clamp(tok.nvalue, 0f32, 100f32)) + return err() + proc parseColor*(val: CSSComponentValue): Opt[CSSColor] = if val of CSSToken: let tok = CSSToken(val) @@ -1073,10 +1125,26 @@ proc parseColor*(val: CSSComponentValue): Opt[CSSColor] = else: discard elif val of CSSFunction: let f = CSSFunction(val) - if f.name in {cftRgb, cftRgba}: - return parseARGB(f.value) - elif f.name == cftChaAnsi: + case f.name + of cftRgb, cftRgba: + let (r, g, b, a, legacy) = ?parseLegacyColorFun(f.value) + if r.t == g.t and g.t == b.t or not legacy: + let r = parseRGBComponent(r) + let g = parseRGBComponent(g) + let b = parseRGBComponent(b) + return ok(rgba(r, g, b, a).cssColor()) + of cftHsl, cftHsla: + let (h, s, l, a, legacy) = ?parseLegacyColorFun(f.value) + if h.t != cttIdent and s.t == cttPercentage and l.t == cttPercentage or + not legacy: + let h = ?parseHue(h) + let s = ?parseSatOrLight(s) + let l = ?parseSatOrLight(l) + return ok(hsla(h, s, l, a).cssColor()) + return err() + of cftChaAnsi: return parseANSI(f.value) + else: discard return err() func parseLength*(val: CSSComponentValue; attrs: WindowAttributes; diff --git a/src/types/color.nim b/src/types/color.nim index 4dda7619..0d5786da 100644 --- a/src/types/color.nim +++ b/src/types/color.nim @@ -1,4 +1,5 @@ import std/algorithm +import std/math import std/options import std/strutils import std/tables @@ -44,14 +45,15 @@ type func rgba*(r, g, b, a: uint8): ARGBColor +# bitmasked so nimvm doesn't choke on it func r*(c: ARGBColor): uint8 = - return uint8(uint32(c) shr 16) + return uint8((uint32(c) shr 16) and 0xFF) func g*(c: ARGBColor): uint8 = - return uint8(uint32(c) shr 8) + return uint8((uint32(c) shr 8) and 0xFF) func b*(c: ARGBColor): uint8 = - return uint8(uint32(c)) + return uint8((uint32(c) and 0xFF)) func a*(c: ARGBColor): uint8 = return uint8(uint32(c) shr 24) @@ -393,6 +395,22 @@ func rgba*(r, g, b, a: int): ARGBColor = func gray*(n: uint8): RGBColor = return rgb(n, n, n) +# ref. https://drafts.csswg.org/css-color/#hsl-to-rgb and +# https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative +func hsla*(h, s, l: float32; a: uint8): ARGBColor = + let h = h mod 360 + let s = float32(s) / 100 + let l = float32(l) / 100 + template f(n: auto): uint8 = + let k = (n + h / 30) mod 12 + let alpha = s * min(l, 1 - l) + let x = l - alpha * max(-1f32, min(k - 3, min(9 - k, 1f32))) + uint8(x * 255) + let r = f(0) + let g = f(8) + let b = f(4) + return rgba(r, g, b, a) + # NOTE: this assumes n notin 0..15 (which would be ANSI 4-bit) func toRGB*(param0: ANSIColor): RGBColor = doAssert uint8(param0) notin 0u8..15u8 diff --git a/test/layout/color-functions.color.expected b/test/layout/color-functions.color.expected index b507512a..a65eb8a9 100644 --- a/test/layout/color-functions.color.expected +++ b/test/layout/color-functions.color.expected @@ -1 +1,13 @@ [38;2;100;200;30mtest[39m +hsl +[38;2;0;200;100mtest[39m +[38;2;255;57;56mtest[39m +test +[38;2;255;40;40mtest[39m +test +[38;2;255;40;40mtest[39m +[38;2;255;40;40mtest[39m +[38;2;144;92;173mtest[39m +[38;2;108;142;61mtest[39m +[38;2;140;100;141mtest[39m +[38;2;236;32;235mtest[39m diff --git a/test/layout/color-functions.html b/test/layout/color-functions.html index 6947300e..2c83c227 100644 --- a/test/layout/color-functions.html +++ b/test/layout/color-functions.html @@ -1,4 +1,14 @@ <!DOCTYPE html> -<div style="color: rgba(100, 200, 30, 1)"> -test -</div> +<div style="color: rgba(100, 200, 30, 1)">test</div> +<div style="color: hsl(40, 20, 41)">hsl</div> +<div style="color: rgb(0 , 200 , 100)">test</div> +<div style="color: rgb(200, 0, 0, 1)">test</div> +<div style="color: rgb(200, 0, 0, 1 test test)">test</div> +<div style="color: rgb(200%, 0%, 0%)">test</div> +<div style="color: rgb(200%, 0%, 0)">test</div> +<div style="color: rgb(200% 0% 0)">test</div> +<div style="color: rgb(200% none none)">test</div> +<div style="color: hsl(999 40 40)">test</div> +<div style="color: hsl(445 40 40)">test</div> +<div style="color: hsl(-445 20 40)">test</div> +<div style="color: hsl(-445 9999 40)">test</div> |