diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index e0db58786..52127b3b7 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -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 diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index f735c8392..6f9af7e08 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -271,26 +271,67 @@ class Color implements _StyleProperty, ColorBase { color = color.substring(0, color.length - 1); // Strip close paren. - var args = []; - 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; + } + + 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; + } + 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(); } } diff --git a/pkgs/csslib/test/color_function_test.dart b/pkgs/csslib/test/color_function_test.dart new file mode 100644 index 000000000..f93268732 --- /dev/null +++ b/pkgs/csslib/test/color_function_test.dart @@ -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); + }); +}