Skip to content

Commit

Permalink
Support Node Sass's sass.types.Color(argb) constructor (#398)
Browse files Browse the repository at this point in the history
Closes #397
  • Loading branch information
nex3 authored Jul 4, 2018
1 parent 5ca8ba3 commit 9bb272d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 26 additions & 5 deletions lib/src/node/value/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.8.0
version: 1.9.0
description: A Sass implementation in Dart.
author: Dart Team <[email protected]>
homepage: https://github.com/sass/dart-sass
Expand Down
31 changes: 21 additions & 10 deletions test/node_api/value/color_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
}

0 comments on commit 9bb272d

Please sign in to comment.