-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathcolor.dart
90 lines (82 loc) · 3.15 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2021 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/nullable.dart';
import '../../util/number.dart';
import '../../value.dart';
import '../reflection.dart';
import '../utils.dart';
/// The JavaScript `SassColor` class.
final JSClass colorClass = () {
var jsClass = createJSClass('sass.SassColor', (Object self, _Channels color) {
if (color.red != null) {
return SassColor.rgb(fuzzyRound(color.red!), fuzzyRound(color.green!),
fuzzyRound(color.blue!), _handleNullAlpha(color.alpha));
} else if (color.saturation != null) {
return SassColor.hsl(color.hue!, color.saturation!, color.lightness!,
_handleNullAlpha(color.alpha));
} else {
return SassColor.hwb(color.hue!, color.whiteness!, color.blackness!,
_handleNullAlpha(color.alpha));
}
});
jsClass.defineMethod('change', (SassColor self, _Channels options) {
if (options.whiteness != null || options.blackness != null) {
return self.changeHwb(
hue: options.hue ?? self.hue,
whiteness: options.whiteness ?? self.whiteness,
blackness: options.blackness ?? self.blackness,
alpha: options.alpha ?? self.alpha);
} else if (options.hue != null ||
options.saturation != null ||
options.lightness != null) {
return self.changeHsl(
hue: options.hue ?? self.hue,
saturation: options.saturation ?? self.saturation,
lightness: options.lightness ?? self.lightness,
alpha: options.alpha ?? self.alpha);
} else if (options.red != null ||
options.green != null ||
options.blue != null) {
return self.changeRgb(
red: options.red.andThen(fuzzyRound) ?? self.red,
green: options.green.andThen(fuzzyRound) ?? self.green,
blue: options.blue.andThen(fuzzyRound) ?? self.blue,
alpha: options.alpha ?? self.alpha);
} else {
return self.changeAlpha(options.alpha ?? self.alpha);
}
});
jsClass.defineGetters({
'red': (SassColor self) => self.red,
'green': (SassColor self) => self.green,
'blue': (SassColor self) => self.blue,
'hue': (SassColor self) => self.hue,
'saturation': (SassColor self) => self.saturation,
'lightness': (SassColor self) => self.lightness,
'whiteness': (SassColor self) => self.whiteness,
'blackness': (SassColor self) => self.blackness,
'alpha': (SassColor self) => self.alpha,
});
getJSClass(SassColor.rgb(0, 0, 0)).injectSuperclass(jsClass);
return jsClass;
}();
/// Converts an undefined [alpha] to 1.
///
/// This ensures that an explicitly null alpha will produce a deprecation
/// warning when passed to the Dart API.
num? _handleNullAlpha(num? alpha) => isUndefined(alpha) ? 1 : alpha;
@JS()
@anonymous
class _Channels {
external num? get red;
external num? get green;
external num? get blue;
external num? get hue;
external num? get saturation;
external num? get lightness;
external num? get whiteness;
external num? get blackness;
external num? get alpha;
}