Support Node Sass's sass.types.Color(argb) constructor (#398)

Closes #397
This commit is contained in:
Natalie Weizenbaum 2018-07-03 17:09:54 -07:00 committed by GitHub
parent 5ca8ba3519
commit 9bb272dcff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 16 deletions

View File

@ -1,3 +1,11 @@
## 1.9.0
### Node API
* Add support for `new sass.types.Color(argb)` for creating colors from ARGB hex
numbers. This was overlooked when initially adding support for Node Sass's
JavaScript API.
## 1.8.0
### Command-Line Interface

View File

@ -21,11 +21,32 @@ Object newNodeSassColor(SassColor value) =>
/// The JS constructor for the `sass.types.Color` class.
final Function colorConstructor = createClass(
(_NodeSassColor thisArg, num red, num green, num blue,
[num alpha, SassColor dartValue]) {
thisArg.dartValue = dartValue ??
new SassColor.rgb(
_clamp(red), _clamp(green), _clamp(blue), alpha?.clamp(0, 1) ?? 1);
(_NodeSassColor thisArg, num redOrArgb,
[num green, num blue, num alpha, SassColor dartValue]) {
if (dartValue != null) {
thisArg.dartValue = dartValue;
return;
}
// This has two signatures:
//
// * `new sass.types.Color(red, green, blue, [alpha])`
// * `new sass.types.Color(argb)`
//
// The latter takes an integer that's interpreted as the hex value 0xAARRGGBB.
num red;
if (green == null) {
var argb = redOrArgb as int;
alpha = (argb >> 24) / 0xff;
red = (argb >> 16) % 0x100;
green = (argb >> 8) % 0x100;
blue = argb % 0x100;
} else {
red = redOrArgb;
}
thisArg.dartValue = new SassColor.rgb(
_clamp(red), _clamp(green), _clamp(blue), alpha?.clamp(0, 1) ?? 1);
}, {
'getR': (_NodeSassColor thisArg) => thisArg.dartValue.red,
'getG': (_NodeSassColor thisArg) => thisArg.dartValue.green,

View File

@ -1,5 +1,5 @@
name: sass
version: 1.8.0
version: 1.9.0
description: A Sass implementation in Dart.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/sass/dart-sass

View File

@ -96,18 +96,29 @@ void main() {
});
group("from a constructor", () {
test("is a color with the given channels", () {
var color = callConstructor(sass.types.Color, [11, 12, 13, 0.42]);
expect(color, isJSInstanceOf(sass.types.Color));
expect(color.getR(), equals(11));
expect(color.getG(), equals(12));
expect(color.getB(), equals(13));
expect(color.getA(), equals(0.42));
group("with individual channels", () {
test("is a color with the given channels", () {
var color = callConstructor(sass.types.Color, [11, 12, 13, 0.42]);
expect(color, isJSInstanceOf(sass.types.Color));
expect(color.getR(), equals(11));
expect(color.getG(), equals(12));
expect(color.getB(), equals(13));
expect(color.getA(), equals(0.42));
});
test("the alpha channel defaults to 1", () {
var color = callConstructor(sass.types.Color, [11, 12, 13]);
expect(color.getA(), equals(1.0));
});
});
test("the alpha channel defaults to 1", () {
var color = callConstructor(sass.types.Color, [11, 12, 13]);
expect(color.getA(), equals(1.0));
test("with an ARGB hex value, is a color with the given channels", () {
var color = callConstructor(sass.types.Color, [0x12345678]);
expect(color, isJSInstanceOf(sass.types.Color));
expect(color.getR(), equals(0x34));
expect(color.getG(), equals(0x56));
expect(color.getB(), equals(0x78));
expect(color.getA(), equals(0x12 / 0xff));
});
});
}