Skip to content

Commit 9bb272d

Browse files
authored
Support Node Sass's sass.types.Color(argb) constructor (#398)
Closes #397
1 parent 5ca8ba3 commit 9bb272d

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.9.0
2+
3+
### Node API
4+
5+
* Add support for `new sass.types.Color(argb)` for creating colors from ARGB hex
6+
numbers. This was overlooked when initially adding support for Node Sass's
7+
JavaScript API.
8+
19
## 1.8.0
210

311
### Command-Line Interface

lib/src/node/value/color.dart

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,32 @@ Object newNodeSassColor(SassColor value) =>
2121

2222
/// The JS constructor for the `sass.types.Color` class.
2323
final Function colorConstructor = createClass(
24-
(_NodeSassColor thisArg, num red, num green, num blue,
25-
[num alpha, SassColor dartValue]) {
26-
thisArg.dartValue = dartValue ??
27-
new SassColor.rgb(
28-
_clamp(red), _clamp(green), _clamp(blue), alpha?.clamp(0, 1) ?? 1);
24+
(_NodeSassColor thisArg, num redOrArgb,
25+
[num green, num blue, num alpha, SassColor dartValue]) {
26+
if (dartValue != null) {
27+
thisArg.dartValue = dartValue;
28+
return;
29+
}
30+
31+
// This has two signatures:
32+
//
33+
// * `new sass.types.Color(red, green, blue, [alpha])`
34+
// * `new sass.types.Color(argb)`
35+
//
36+
// The latter takes an integer that's interpreted as the hex value 0xAARRGGBB.
37+
num red;
38+
if (green == null) {
39+
var argb = redOrArgb as int;
40+
alpha = (argb >> 24) / 0xff;
41+
red = (argb >> 16) % 0x100;
42+
green = (argb >> 8) % 0x100;
43+
blue = argb % 0x100;
44+
} else {
45+
red = redOrArgb;
46+
}
47+
48+
thisArg.dartValue = new SassColor.rgb(
49+
_clamp(red), _clamp(green), _clamp(blue), alpha?.clamp(0, 1) ?? 1);
2950
}, {
3051
'getR': (_NodeSassColor thisArg) => thisArg.dartValue.red,
3152
'getG': (_NodeSassColor thisArg) => thisArg.dartValue.green,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sass
2-
version: 1.8.0
2+
version: 1.9.0
33
description: A Sass implementation in Dart.
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/sass/dart-sass

test/node_api/value/color_test.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,29 @@ void main() {
9696
});
9797

9898
group("from a constructor", () {
99-
test("is a color with the given channels", () {
100-
var color = callConstructor(sass.types.Color, [11, 12, 13, 0.42]);
101-
expect(color, isJSInstanceOf(sass.types.Color));
102-
expect(color.getR(), equals(11));
103-
expect(color.getG(), equals(12));
104-
expect(color.getB(), equals(13));
105-
expect(color.getA(), equals(0.42));
99+
group("with individual channels", () {
100+
test("is a color with the given channels", () {
101+
var color = callConstructor(sass.types.Color, [11, 12, 13, 0.42]);
102+
expect(color, isJSInstanceOf(sass.types.Color));
103+
expect(color.getR(), equals(11));
104+
expect(color.getG(), equals(12));
105+
expect(color.getB(), equals(13));
106+
expect(color.getA(), equals(0.42));
107+
});
108+
109+
test("the alpha channel defaults to 1", () {
110+
var color = callConstructor(sass.types.Color, [11, 12, 13]);
111+
expect(color.getA(), equals(1.0));
112+
});
106113
});
107114

108-
test("the alpha channel defaults to 1", () {
109-
var color = callConstructor(sass.types.Color, [11, 12, 13]);
110-
expect(color.getA(), equals(1.0));
115+
test("with an ARGB hex value, is a color with the given channels", () {
116+
var color = callConstructor(sass.types.Color, [0x12345678]);
117+
expect(color, isJSInstanceOf(sass.types.Color));
118+
expect(color.getR(), equals(0x34));
119+
expect(color.getG(), equals(0x56));
120+
expect(color.getB(), equals(0x78));
121+
expect(color.getA(), equals(0x12 / 0xff));
111122
});
112123
});
113124
}

0 commit comments

Comments
 (0)