/
color.dart
72 lines (64 loc) · 2.43 KB
/
color.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:js/js.dart';
import '../../../util/number.dart';
import '../../../value.dart';
import '../../reflection.dart';
@JS()
class _NodeSassColor {
external SassColor get dartValue;
external set dartValue(SassColor dartValue);
}
/// Creates a new `sass.types.Color` object wrapping [value].
Object newNodeSassColor(SassColor value) =>
legacyColorClass.construct([null, null, null, null, value]);
/// The JS `sass.types.Color` class.
final JSClass legacyColorClass = createJSClass('sass.types.Color',
(_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 || blue == null) {
var argb = redOrArgb as int;
alpha = (argb >> 24) / 0xff;
red = (argb >> 16) % 0x100;
green = (argb >> 8) % 0x100;
blue = argb % 0x100;
} else {
// Either [dartValue] or [redOrArgb] must be passed.
red = redOrArgb!;
}
thisArg.dartValue = SassColor.rgb(
_clamp(red), _clamp(green), _clamp(blue), alpha?.clamp(0, 1) ?? 1);
})
..defineMethods({
'getR': (_NodeSassColor thisArg) => thisArg.dartValue.red,
'getG': (_NodeSassColor thisArg) => thisArg.dartValue.green,
'getB': (_NodeSassColor thisArg) => thisArg.dartValue.blue,
'getA': (_NodeSassColor thisArg) => thisArg.dartValue.alpha,
'setR': (_NodeSassColor thisArg, num value) {
thisArg.dartValue = thisArg.dartValue.changeRgb(red: _clamp(value));
},
'setG': (_NodeSassColor thisArg, num value) {
thisArg.dartValue = thisArg.dartValue.changeRgb(green: _clamp(value));
},
'setB': (_NodeSassColor thisArg, num value) {
thisArg.dartValue = thisArg.dartValue.changeRgb(blue: _clamp(value));
},
'setA': (_NodeSassColor thisArg, num value) {
thisArg.dartValue = thisArg.dartValue.changeRgb(alpha: value.clamp(0, 1));
}
});
/// Clamps [channel] within the range 0, 255 and rounds it to the nearest
/// integer.
int _clamp(num channel) => fuzzyRound(channel.clamp(0, 255));