forked from adobe-webplatform/Snap.svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.js
179 lines (178 loc) · 6.1 KB
/
colors.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
describe("Colours", function () {
it("parses hex colour", function () {
expect(Snap.color("#fC0").hex).to.be("#ffcc00");
});
it("parses RGB", function () {
var c = Snap.color("rgb(255, 204, 0)");
expect(c.hex).to.be("#ffcc00");
expect(c.r).to.be(255);
expect(c.g).to.be(204);
expect(c.b).to.be(0);
});
it("parses RGB - %", function () {
var c = Snap.color("rgb(100%, 80%, 0%)");
expect(c.hex).to.be("#ffcc00");
expect(c.r).to.be(255);
expect(c.g).to.be(204);
expect(c.b).to.be(0);
});
it("parses HSL", function () {
var c = Snap.color("hsl(0.1333, 1, .5)");
expect(c.hex).to.be("#ffcc00");
expect(c.h.toFixed(3)).to.be("0.133");
expect(c.s).to.be(1);
expect(c.l).to.be(.5);
});
it("parses HSL - %", function () {
var c = Snap.color("hsl(13.33%, 100%, 50%)");
expect(c.hex).to.be("#ffcc00");
expect(c.h.toFixed(3)).to.be("0.133");
expect(c.s).to.be(1);
expect(c.l).to.be(.5);
});
it("parses HSB", function () {
var c = Snap.color("hsb(0.1333, 1, 1)");
expect(c.hex).to.be("#ffcc00");
expect(c.h.toFixed(3)).to.be("0.133");
expect(c.s).to.be(1);
expect(c.v).to.be(1);
});
it("parses HSB - %", function () {
var c = Snap.color("hsb(13.33%, 100%, 100%)");
expect(c.hex).to.be("#ffcc00");
expect(c.h.toFixed(3)).to.be("0.133");
expect(c.s).to.be(1);
expect(c.v).to.be(1);
});
it("parses RGBA", function () {
var c = Snap.color("rgba(255, 204, 0, .75)");
expect(c.hex).to.be("#ffcc00");
expect(c.r).to.be(255);
expect(c.g).to.be(204);
expect(c.b).to.be(0);
expect(c.opacity).to.be(.75);
});
it("parses HSLA", function () {
var c = Snap.color("hsla(0.1333, 1, .5, .5)");
expect(c.hex).to.be("#ffcc00");
expect(c.r).to.be(255);
expect(c.g).to.be(204);
expect(c.b).to.be(0);
expect(c.opacity).to.be(.5);
});
it("parses HSBA", function () {
var c = Snap.color("hsba(0.1333, 1, 1, .5)");
expect(c.hex).to.be("#ffcc00");
expect(c.r).to.be(255);
expect(c.g).to.be(204);
expect(c.b).to.be(0);
expect(c.opacity).to.be(.5);
});
it("parses names", function () {
var c = Snap.color("DodgerBlue");
expect(c.hex).to.be("#1e90ff");
c = Snap.color("FireBrick");
expect(c.hex).to.be("#b22222");
c = Snap.color("MintCream");
expect(c.hex).to.be("#f5fffa");
});
it("parses to RGB", function() {
var expectRGB = function(str) {
var c = Snap.getRGB(str);
expect(c.hex).to.be("#ffcc00");
expect(c.r).to.be(255);
expect(c.g).to.be(204);
expect(c.b).to.be(0);
expect(c.error).to.not.be(true);
};
expectRGB("#fc0");
expectRGB("#ffcc00");
expectRGB("rgb(255, 204, 0)");
expectRGB("rgb(100%, 80%, 0%)");
expectRGB("hsb(0.1333, 1, 1)");
expectRGB("hsb(13.33%, 100%, 100%)");
expectRGB("hsl(0.1333, 1, .5)");
expectRGB("hsl(13.33%, 100%, 50%)");
expectRGB("rgba(255, 204, 0, .75)");
expectRGB("hsla(0.1333, 1, .5, .5)");
expectRGB("hsba(0.1333, 1, 1, .5)");
var c = Snap.getRGB("DodgerBlue");
expect(c.hex).to.be("#1e90ff");
expect(c.r).to.be(30);
expect(c.g).to.be(144);
expect(c.b).to.be(255);
expect(c.error).to.not.be(true);
c = Snap.getRGB("foobar");
expect(!!c.error).to.be(true);
c = Snap.getRGB("#zzz");
expect(!!c.error).to.be(true);
c = Snap.getRGB("rgb(255)");
expect(!!c.error).to.be(true);
c = Snap.getRGB("hsl(0, 0, 0");
expect(!!c.error).to.be(true);
c = Snap.getRGB("rab(0, 0, 0)");
expect(!!c.error).to.be(true);
});
it("creates hsb", function() {
var str = Snap.hsb(0.13333, 1, 1);
expect(str).to.be("#ffcc00");
str = Snap.hsb(0, 0.5, 0.5);
expect(str).to.be("#804040");
});
it("creates rgb from hsb", function() {
var rgb = Snap.hsb2rgb(0.13333, 1, 1);
expect(rgb.r).to.be(255);
expect(rgb.g).to.be(204);
expect(rgb.b).to.be(0);
expect(rgb.hex).to.be("#ffcc00");
rgb = Snap.hsb2rgb(0, 0.5, 0.5);
expect(rgb.r).to.be(128);
expect(rgb.g).to.be(64);
expect(rgb.b).to.be(64);
expect(rgb.hex).to.be("#804040");
});
it("creates hsl", function() {
var str = Snap.hsl(0.13333, 1, 0.5);
expect(str).to.be("#ffcc00");
str = Snap.hsl(0, 1, 0.75);
expect(str).to.be("#ff8080");
});
it("creates rgb from hsl", function() {
var rgb = Snap.hsl2rgb(0.13333, 1, 0.5);
expect(rgb.r).to.be(255);
expect(rgb.g).to.be(204);
expect(rgb.b).to.be(0);
expect(rgb.hex).to.be("#ffcc00");
rgb = Snap.hsl2rgb(0, 1, 0.75);
expect(rgb.r).to.be(255);
expect(rgb.g).to.be(128);
expect(rgb.b).to.be(128);
expect(rgb.hex).to.be("#ff8080");
});
it("creates rgb", function() {
var str = Snap.rgb(255, 204, 0);
expect(str).to.be("#ffcc00");
str = Snap.rgb(64, 128, 255);
expect(str).to.be("#4080ff");
});
it("creates hsb from rgb", function() {
var hsb = Snap.rgb2hsb(255, 204, 0);
expect(hsb.h.toFixed(3)).to.be("0.133");
expect(hsb.s).to.be(1);
expect(hsb.b).to.be(1);
hsb = Snap.rgb2hsb(128, 64, 64);
expect(hsb.h).to.be(0);
expect(hsb.s.toFixed(1)).to.be("0.5");
expect(hsb.b.toFixed(1)).to.be("0.5");
});
it("creates hsl from rgb", function() {
var hsl = Snap.rgb2hsl(255, 204, 0);
expect(hsl.h.toFixed(3)).to.be("0.133");
expect(hsl.s).to.be(1);
expect(hsl.l).to.be(0.5);
hsl = Snap.rgb2hsl(255, 128, 128);
expect(hsl.h).to.be(0);
expect(hsl.s).to.be(1);
expect(hsl.l.toFixed(2)).to.be("0.75");
});
});