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
4 changes: 4 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 1.0.3-wip

- `Color.css` now reads the `hsl()`/`hsla()` hue as degrees (optional `deg`)
instead of a 0..1 fraction, and accepts percentages for saturation, lightness
and alpha. Functional colors with the wrong number of components or invalid
or non-finite values now throw a `FormatException`.
- 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
Expand Down
65 changes: 53 additions & 12 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,26 +271,67 @@ class Color implements _StyleProperty, ColorBase {

color = color.substring(0, color.length - 1); // Strip close paren.

var args = <num>[];
var params = color.split(',');
for (var param in params) {
args.add(double.parse(param));
final params = color.split(',');
final hasAlpha = type == _rgbaCss || type == _hslaCss;
if (params.length != (hasAlpha ? 4 : 3)) {
throw FormatException('Invalid CSS color component count', value);
}

double parseNumber(String text) {
final number = double.tryParse(text.trim());
if (number == null || !number.isFinite) {
throw FormatException('Invalid CSS color component', value);
}
return number;
}
Comment thread
palmoni5 marked this conversation as resolved.
Resolved

double parseFraction(String text) {
final trimmed = text.trim();
if (trimmed.endsWith('%')) {
return parseNumber(trimmed.substring(0, trimmed.length - 1)) / 100;
}
return parseNumber(trimmed);
}

int parseRgbChannel(String text) => parseNumber(text).toInt();

double parseHue(String text) {
final trimmed = text.trim().toLowerCase();
final degrees = trimmed.endsWith('deg')
? parseNumber(trimmed.substring(0, trimmed.length - 3))
: parseNumber(trimmed);
// CSS hue is an angle, not the 0..1 fraction expected by Hsla.
// Dart's `%` is never negative here, so -120 wraps to 240 too.
return degrees % 360 / 360;
}
Comment thread
palmoni5 marked this conversation as resolved.
Resolved

switch (type) {
case _rgbCss:
return Color.convertToHexString(
args[0].toInt(), args[1].toInt(), args[2].toInt());
parseRgbChannel(params[0]),
parseRgbChannel(params[1]),
parseRgbChannel(params[2]),
);
case _rgbaCss:
return Color.convertToHexString(
args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3]);
parseRgbChannel(params[0]),
parseRgbChannel(params[1]),
parseRgbChannel(params[2]),
parseFraction(params[3]),
);
case _hslCss:
return Hsla(args[0], args[1], args[2]).toHexArgbString();
return Hsla(
parseHue(params[0]),
parseFraction(params[1]),
parseFraction(params[2]),
).toHexArgbString();
case _hslaCss:
return Hsla(args[0], args[1], args[2], args[3]).toHexArgbString();
default:
// Type not defined UnsupportedOperationException should have thrown.
assert(false);
break;
return Hsla(
parseHue(params[0]),
parseFraction(params[1]),
parseFraction(params[2]),
parseFraction(params[3]),
).toHexArgbString();
}
}

Expand Down
36 changes: 36 additions & 0 deletions pkgs/csslib/test/color_function_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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('HSL function converts degrees and percentages', () {
expect(Color.css('hsl(0, 100%, 50%)'), Color(0xff0000));
expect(Color.css('hsl(120, 100%, 50%)'), Color(0x00ff00));
expect(Color.css('hsl(240, 100%, 50%)'), Color(0x0000ff));
});

test('HSL hue wraps and respects CSS angle units', () {
expect(Color.css('hsl(480deg,100%,50%)'), Color(0x00ff00));
expect(Color.css('hsl(-120deg,100%,50%)'), Color(0x0000ff));
expect(Color.css('HSL(120DEG,100%,50%)'), Color(0x00ff00));
});

test('HSLA parses fractional and percentage alpha', () {
expect(
Color.css('hsla(240,100%,50%,0.5)').toHexArgbString(),
'800000ff',
);
expect(
Color.css('hsla(240,100%,50%,50%)').toHexArgbString(),
'800000ff',
);
});

test('invalid color arity and nonfinite components fail cleanly', () {
expect(() => Color.css('rgba(10,20,30)'), throwsFormatException);
expect(() => Color.css('rgb(NaN,20,30)'), throwsFormatException);
});
}
Loading