Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
33 changes: 14 additions & 19 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Comment thread
palmoni5 marked this conversation as resolved.
Resolved

factory Rgba.fromHsla(Hsla hsla) {
// Convert to Rgba.
Expand Down Expand Up @@ -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;
Comment thread
palmoni5 marked this conversation as resolved.
Resolved

Color get color => Color.createRgba(r, g, b, a);
Hsla get hsla => Hsla.fromRgba(this);
Expand Down Expand Up @@ -697,15 +692,15 @@ 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;
s = vm;
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;
Expand Down Expand Up @@ -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));
Expand Down
35 changes: 35 additions & 0 deletions pkgs/csslib/test/color_conversion_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
}
Loading