From 2884aabb9451129f2658bf3b697f462b3080889b Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 17:26:54 +0200 Subject: [PATCH 1/3] [csslib] Fix Rgba/Hsla ARGB conversions and keep exact alpha --- pkgs/csslib/CHANGELOG.md | 5 ++ pkgs/csslib/lib/src/property.dart | 52 ++++++++++---------- pkgs/csslib/test/color_conversion_test.dart | 54 +++++++++++++++++++++ 3 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 pkgs/csslib/test/color_conversion_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..1529eaca2c 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,10 @@ ## 1.0.3-wip +- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, preserve alpha + for grayscale colors in `Hsla.fromRgba`, and keep the exact 8-bit alpha in + `Color.rgba`, `Color.hsla` and `Hsla.fromArgbValue` instead of rounding it to + two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, + and `Color.css` throws a `FormatException` for unsupported colors. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 0188c2b8e2..39cd05e427 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -71,7 +71,11 @@ class Color implements _StyleProperty, ColorBase { /// Creates a new color from a CSS color string. For more information, see /// . - Color.css(String color) : _argb = Color._convertCssToArgb(color)!; + /// + /// Throws a [FormatException] if [color] is not a supported CSS color. + Color.css(String color) + : _argb = Color._convertCssToArgb(color) ?? + (throw FormatException('Unsupported CSS color', color)); // TODO(jmesserly): I found the use of percents a bit suprising. /// HSL takes three values. The [hueDegree] degree on the color wheel; '0' is @@ -148,7 +152,7 @@ class Color implements _StyleProperty, ColorBase { // Get alpha blending value 0..255 var alpha = Color.hexToInt(_argb.substring(nextIndex, nextIndex + 2)); // Convert to value from 0..1 - a = double.parse((alpha / 255).toStringAsPrecision(2)); + a = alpha / 255; nextIndex += 2; } var r = Color.hexToInt(_argb.substring(nextIndex, nextIndex + 2)); @@ -499,17 +503,19 @@ class Rgba implements _StyleProperty, ColorBase { b = Color._clamp(blue, 0, 255), a = (alpha != null) ? Color._clamp(alpha, 0, 1) : alpha; - factory Rgba.fromString(String hexValue) => - Color.css('#${Color._convertCssToArgb(hexValue)}').rgba; + factory Rgba.fromString(String cssColor) => Color.css(cssColor).rgba; factory Rgba.fromColor(Color color) => color.rgba; - factory Rgba.fromArgbValue(num value) => Rgba( - (value.toInt() & 0xff000000) >> 0x18, // a - (value.toInt() & 0xff0000) >> 0x10, // r - (value.toInt() & 0xff00) >> 8, // g - value.toInt() & 0xff, - ); // b + factory Rgba.fromArgbValue(num value) { + final argb = value.toInt(); + return Rgba( + (argb >> 16) & 0xff, + (argb >> 8) & 0xff, + argb & 0xff, + ((argb >> 24) & 0xff) / 255, + ); + } factory Rgba.fromHsla(Hsla hsla) { // Convert to Rgba. @@ -587,16 +593,8 @@ class Rgba implements _StyleProperty, ColorBase { String toHexArgbString() => Color.convertToHexString(r, g, b, a); @override - int get argbValue { - var value = 0; - if (a != null) { - value = a!.toInt() << 0x18; - } - value += r << 0x10; - value += g << 0x08; - value += b; - return value; - } + int get argbValue => + ((a == null ? 0 : (a! * 255).round()) << 24) | (r << 16) | (g << 8) | b; Color get color => Color.createRgba(r, g, b, a); Hsla get hsla => Hsla.fromRgba(this); @@ -628,8 +626,8 @@ class Hsla implements _StyleProperty, ColorBase { _l = Color._clamp(lightness, 0, 1), _a = (alpha != null) ? Color._clamp(alpha, 0, 1) : alpha; - factory Hsla.fromString(String hexValue) { - var rgba = Color.css('#${Color._convertCssToArgb(hexValue)}').rgba; + factory Hsla.fromString(String cssColor) { + final rgba = Color.css(cssColor).rgba; return _createFromRgba(rgba.r, rgba.g, rgba.b, rgba.a); } @@ -644,8 +642,8 @@ class Hsla implements _StyleProperty, ColorBase { var g = (value.toInt() & 0xff00) >> 8; var b = value.toInt() & 0xff; - // Convert alpha to 0..1 from (0..255). - a = double.parse((a / 255).toStringAsPrecision(2)); + // Keep the full 8-bit alpha precision for lossless ARGB round-trips. + a /= 255; return _createFromRgba(r, g, b, a); } @@ -670,7 +668,7 @@ class Hsla implements _StyleProperty, ColorBase { var maxRgb = math.max(r, math.max(g, b)); l = (maxRgb + minRgb) / 2; if (l <= 0) { - return Hsla(0, 0, l); // Black; + return Hsla(0, 0, l, a); // Black; } var vm = maxRgb - minRgb; @@ -678,7 +676,7 @@ class Hsla implements _StyleProperty, ColorBase { if (s > 0) { s /= (l < 0.5) ? (maxRgb + minRgb) : (2 - maxRgb - minRgb); } else { - return Hsla(0, 0, l); // White + return Hsla(0, 0, l, a); // White } num r2, g2, b2; @@ -732,7 +730,7 @@ class Hsla implements _StyleProperty, ColorBase { @override int get argbValue => Color.hexToInt(toHexArgbString()); - Color get color => Color.createHsla(_h, _s, _l, _a); + Color get color => Rgba.fromHsla(this).color; Rgba get rgba => Rgba.fromHsla(this); Hsla darker(num amount) => Hsla.fromRgba(Rgba.fromHsla(this).darker(amount)); diff --git a/pkgs/csslib/test/color_conversion_test.dart b/pkgs/csslib/test/color_conversion_test.dart new file mode 100644 index 0000000000..e4cb0d05a3 --- /dev/null +++ b/pkgs/csslib/test/color_conversion_test.dart @@ -0,0 +1,54 @@ +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:csslib/parser.dart'; +import 'package:test/test.dart'; + +void main() { + test('ARGB channel extraction preserves all four channels', () { + final rgba = Rgba.fromArgbValue(0x804080c0); + expect(rgba.r, 0x40); + expect(rgba.g, 0x80); + expect(rgba.b, 0xc0); + expect(rgba.a, closeTo(128 / 255, 0.00001)); + expect(rgba.argbValue, 0x804080c0); + expect(rgba.toHexArgbString(), '804080c0'); + }); + + test('RGBA fractional alpha is encoded into 8-bit ARGB', () { + expect(Rgba(0xff, 0, 0, 0.5).argbValue, 0x80ff0000); + expect(Rgba(0xff, 0, 0, 0).argbValue, 0x00ff0000); + expect(Rgba(0xff, 0, 0, 1).argbValue, 0xffff0000); + }); + + test('ARGB round-trips retain each alpha byte, including 0x7f', () { + expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233'); + expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080'); + }); + + test('color string factories decode CSS colors only once', () { + expect(Rgba.fromString('rgba(255,0,0,0.5)').toHexArgbString(), '80ff0000'); + expect( + Hsla.fromString('rgba(255,0,0,0.5)').color.toHexArgbString(), + '80ff0000', + ); + expect(Rgba.fromString('rgb(255,0,0)').r, 255); + }); + + test('color string factories reject unsupported colors', () { + expect(() => Rgba.fromString('foo'), throwsFormatException); + expect(() => Hsla.fromString('foo'), throwsFormatException); + }); + + test('HSL to Color preserves hue and saturation', () { + expect(Hsla(0, 1, 0.5, 1).color.toHexArgbString(), 'ffff0000'); + expect(Hsla(1 / 3, 1, 0.5).color.toHexArgbString(), '00ff00'); + }); + + test('grayscale HSL conversion preserves alpha', () { + final gray = Hsla.fromRgba(Rgba(128, 128, 128, 0.5)); + expect(gray.alpha, 0.5); + expect(gray.color.toHexArgbString(), '80808080'); + }); +} From 2f97d68da884d3d1e2d318545b0fbdd8505e61f0 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 19:30:41 +0200 Subject: [PATCH 2/3] Cover alpha bytes >= 0x80 in ARGB round-trip tests --- pkgs/csslib/test/color_conversion_test.dart | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pkgs/csslib/test/color_conversion_test.dart b/pkgs/csslib/test/color_conversion_test.dart index e4cb0d05a3..b573fd3fe5 100644 --- a/pkgs/csslib/test/color_conversion_test.dart +++ b/pkgs/csslib/test/color_conversion_test.dart @@ -22,10 +22,17 @@ void main() { expect(Rgba(0xff, 0, 0, 1).argbValue, 0xffff0000); }); - test('ARGB round-trips retain each alpha byte, including 0x7f', () { - expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233'); - expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080'); - }); + test( + 'ARGB round-trips retain each alpha byte, including 0x7f and >= 0x80', + () { + expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233'); + expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080'); + expect(Hsla.fromArgbValue(0x80808080).toHexArgbString(), '80808080'); + expect(Hsla.fromArgbValue(0xff808080).toHexArgbString(), 'ff808080'); + expect(Rgba.fromArgbValue(0xff808080).argbValue, 0xff808080); + expect(Rgba.fromArgbValue(0x80808080).argbValue, 0x80808080); + }, + ); test('color string factories decode CSS colors only once', () { expect(Rgba.fromString('rgba(255,0,0,0.5)').toHexArgbString(), '80ff0000'); From 25a9a58401934249b10e8ccbc711a14b40bb4a8f Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:38:12 +0200 Subject: [PATCH 3/3] Address review feedback --- pkgs/csslib/CHANGELOG.md | 7 ++---- pkgs/csslib/lib/src/property.dart | 19 +++++++-------- pkgs/csslib/test/color_conversion_test.dart | 26 --------------------- 3 files changed, 10 insertions(+), 42 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 1529eaca2c..38652a78ac 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,10 +1,7 @@ ## 1.0.3-wip -- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, preserve alpha - for grayscale colors in `Hsla.fromRgba`, and keep the exact 8-bit alpha in - `Color.rgba`, `Color.hsla` and `Hsla.fromArgbValue` instead of rounding it to - two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, - and `Color.css` throws a `FormatException` for unsupported colors. +- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, and preserve + alpha for grayscale colors in `Hsla.fromRgba`. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 39cd05e427..de44cb9c03 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -71,11 +71,7 @@ class Color implements _StyleProperty, ColorBase { /// Creates a new color from a CSS color string. For more information, see /// . - /// - /// Throws a [FormatException] if [color] is not a supported CSS color. - Color.css(String color) - : _argb = Color._convertCssToArgb(color) ?? - (throw FormatException('Unsupported CSS color', color)); + Color.css(String color) : _argb = Color._convertCssToArgb(color)!; // TODO(jmesserly): I found the use of percents a bit suprising. /// HSL takes three values. The [hueDegree] degree on the color wheel; '0' is @@ -152,7 +148,7 @@ class Color implements _StyleProperty, ColorBase { // Get alpha blending value 0..255 var alpha = Color.hexToInt(_argb.substring(nextIndex, nextIndex + 2)); // Convert to value from 0..1 - a = alpha / 255; + a = double.parse((alpha / 255).toStringAsPrecision(2)); nextIndex += 2; } var r = Color.hexToInt(_argb.substring(nextIndex, nextIndex + 2)); @@ -503,7 +499,8 @@ class Rgba implements _StyleProperty, ColorBase { b = Color._clamp(blue, 0, 255), a = (alpha != null) ? Color._clamp(alpha, 0, 1) : alpha; - factory Rgba.fromString(String cssColor) => Color.css(cssColor).rgba; + factory Rgba.fromString(String hexValue) => + Color.css('#${Color._convertCssToArgb(hexValue)}').rgba; factory Rgba.fromColor(Color color) => color.rgba; @@ -626,8 +623,8 @@ class Hsla implements _StyleProperty, ColorBase { _l = Color._clamp(lightness, 0, 1), _a = (alpha != null) ? Color._clamp(alpha, 0, 1) : alpha; - factory Hsla.fromString(String cssColor) { - final rgba = Color.css(cssColor).rgba; + factory Hsla.fromString(String hexValue) { + var rgba = Color.css('#${Color._convertCssToArgb(hexValue)}').rgba; return _createFromRgba(rgba.r, rgba.g, rgba.b, rgba.a); } @@ -642,8 +639,8 @@ class Hsla implements _StyleProperty, ColorBase { var g = (value.toInt() & 0xff00) >> 8; var b = value.toInt() & 0xff; - // Keep the full 8-bit alpha precision for lossless ARGB round-trips. - a /= 255; + // Convert alpha to 0..1 from (0..255). + a = double.parse((a / 255).toStringAsPrecision(2)); return _createFromRgba(r, g, b, a); } diff --git a/pkgs/csslib/test/color_conversion_test.dart b/pkgs/csslib/test/color_conversion_test.dart index b573fd3fe5..a4804f7763 100644 --- a/pkgs/csslib/test/color_conversion_test.dart +++ b/pkgs/csslib/test/color_conversion_test.dart @@ -22,32 +22,6 @@ void main() { expect(Rgba(0xff, 0, 0, 1).argbValue, 0xffff0000); }); - test( - 'ARGB round-trips retain each alpha byte, including 0x7f and >= 0x80', - () { - expect(const Color.hex('7f112233').rgba.toHexArgbString(), '7f112233'); - expect(Hsla.fromArgbValue(0x7f808080).toHexArgbString(), '7f808080'); - expect(Hsla.fromArgbValue(0x80808080).toHexArgbString(), '80808080'); - expect(Hsla.fromArgbValue(0xff808080).toHexArgbString(), 'ff808080'); - expect(Rgba.fromArgbValue(0xff808080).argbValue, 0xff808080); - expect(Rgba.fromArgbValue(0x80808080).argbValue, 0x80808080); - }, - ); - - test('color string factories decode CSS colors only once', () { - expect(Rgba.fromString('rgba(255,0,0,0.5)').toHexArgbString(), '80ff0000'); - expect( - Hsla.fromString('rgba(255,0,0,0.5)').color.toHexArgbString(), - '80ff0000', - ); - expect(Rgba.fromString('rgb(255,0,0)').r, 255); - }); - - test('color string factories reject unsupported colors', () { - expect(() => Rgba.fromString('foo'), throwsFormatException); - expect(() => Hsla.fromString('foo'), throwsFormatException); - }); - test('HSL to Color preserves hue and saturation', () { expect(Hsla(0, 1, 0.5, 1).color.toHexArgbString(), 'ffff0000'); expect(Hsla(1 / 3, 1, 0.5).color.toHexArgbString(), '00ff00');