diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index d1d1212f9..e0db58786 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,7 @@ ## 1.0.3-wip +- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, and preserve + alpha for grayscale colors in `Hsla.fromRgba`. - Fix fractional alpha in the numeric `Color` constructor and normalize CSS short and eight-digit hex colors, with validation of malformed input and unknown color names. Eight-digit hex colors are now read as `#rrggbbaa` diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 9ce36bf6e..f735c8392 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -531,12 +531,15 @@ class Rgba implements _StyleProperty, ColorBase { 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. @@ -614,16 +617,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); @@ -697,7 +692,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; @@ -705,7 +700,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; @@ -759,7 +754,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 000000000..a4804f776 --- /dev/null +++ b/pkgs/csslib/test/color_conversion_test.dart @@ -0,0 +1,35 @@ +// 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('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'); + }); +}