Skip to content

Commit 0aa6c66

Browse files
committed
Start refactoring Color tests.
1 parent a48e6e5 commit 0aa6c66

5 files changed

Lines changed: 81 additions & 137 deletions

File tree

test/lib/helpers.js

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function asyncTest(testName, expected) {
6060
}
6161

6262
function compareNumbers(number1, number2, message) {
63-
equals(Format.number(number1, 2), Format.number(number2, 2), message);
63+
equals(Format.number(number1, 5), Format.number(number2, 5), message);
6464
}
6565

6666
function comparePoints(point1, point2, message) {
@@ -81,40 +81,11 @@ function compareRectangles(rect1, rect2, message) {
8181
(message || '') + ' height');
8282
}
8383

84-
function compareRgbColors(color1, color2, message) {
84+
function compareColors(color1, color2, message) {
8585
color1 = new Color(color1);
8686
color2 = new Color(color2);
87-
88-
compareNumbers(color1.red, color2.red,
89-
(message || '') + ' red');
90-
compareNumbers(color1.green, color2.green,
91-
(message || '') + ' green');
92-
compareNumbers(color1.blue, color2.blue,
93-
(message || '') + ' blue');
94-
compareNumbers(color1.alpha, color2.alpha,
95-
(message || '') + ' alpha');
96-
}
97-
98-
function compareHsbColors(color1, color2, message) {
99-
color1 = new Color(color1);
100-
color2 = new Color(color2);
101-
102-
compareNumbers(color1.hue, color2.hue,
103-
(message || '') + ' hue');
104-
compareNumbers(color1.saturation, color2.saturation,
105-
(message || '') + ' saturation');
106-
compareNumbers(color1.brightness, color2.brightness,
107-
(message || '') + ' brightness');
108-
compareNumbers(color1.alpha, color2.alpha,
109-
(message || '') + ' alpha');
110-
}
111-
112-
function compareGrayColors(color1, color2, message) {
113-
color1 = new Color(color1);
114-
color2 = new Color(color2);
115-
116-
compareNumbers(color1.gray, color2.gray,
117-
(message || '') + ' gray');
87+
equals(color1.type, color2.type, (message || '') + ' type');
88+
equals(color1.components.toString(), color2.components.toString(), (message || '') + ' components');
11889
}
11990

12091
function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {

test/tests/Color.js

Lines changed: 46 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,161 +15,134 @@ module('Color');
1515
test('Set named color', function() {
1616
var path = new Path();
1717
path.fillColor = 'red';
18-
compareRgbColors(path.fillColor, new Color(1, 0, 0));
18+
compareColors(path.fillColor, new Color(1, 0, 0));
1919
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
2020
});
2121

2222
test('Set color to hex', function() {
2323
var path = new Path();
2424
path.fillColor = '#ff0000';
25-
compareRgbColors(path.fillColor, new Color(1, 0, 0));
25+
compareColors(path.fillColor, new Color(1, 0, 0));
2626
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
2727

2828
var path = new Path();
2929
path.fillColor = '#f00';
30-
compareRgbColors(path.fillColor, new Color(1, 0, 0));
30+
compareColors(path.fillColor, new Color(1, 0, 0));
3131
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
3232
});
3333

3434
test('Set color to object', function() {
3535
var path = new Path();
3636
path.fillColor = { red: 1, green: 0, blue: 1};
37-
compareRgbColors(path.fillColor, new Color(1, 0, 1));
37+
compareColors(path.fillColor, new Color(1, 0, 1));
3838
equals(path.fillColor.toCss(), 'rgb(255, 0, 255)');
3939

4040
var path = new Path();
4141
path.fillColor = { gray: 0.2 };
42-
compareRgbColors(path.fillColor, new Color(0.8, 0.8, 0.8));
42+
compareColors(path.fillColor, new Color(0.2));
4343
equals(path.fillColor.toCss(), 'rgb(204, 204, 204)');
4444
});
4545

4646
test('Set color to array', function() {
4747
var path = new Path();
4848
path.fillColor = [1, 0, 0];
49-
compareRgbColors(path.fillColor, new Color(1, 0, 0));
49+
compareColors(path.fillColor, new Color(1, 0, 0));
5050
equals(path.fillColor.toCss(), 'rgb(255, 0, 0)');
5151
});
5252

5353
test('Creating colors', function() {
5454

55-
compareRgbColors(new Color('#ff0000'), new Color(1, 0, 0),
56-
'RgbColor from hex code');
55+
compareColors(new Color('#ff0000'), new Color(1, 0, 0),
56+
'Color from hex code');
5757

58-
compareRgbColors(new Color({ red: 1, green: 0, blue: 1}),
59-
new Color(1, 0, 1), 'RgbColor from rgb object literal');
58+
compareColors(new Color({ red: 1, green: 0, blue: 1}),
59+
new Color(1, 0, 1), 'Color from rgb object literal');
6060

61-
compareRgbColors(new Color({ gray: 0.2 }),
62-
new Color(0.8, 0.8, 0.8), 'RgbColor from gray object literal');
61+
compareColors(new Color({ gray: 0.2 }),
62+
new Color(0.8, 0.8, 0.8).convert('gray'), 'Color from gray object literal');
6363

64-
compareRgbColors(new Color({ hue: 0, saturation: 1, brightness: 1}),
65-
new Color(1, 0, 0), 'RgbColor from hsb object literal');
64+
compareColors(new Color({ hue: 0, saturation: 1, brightness: 1}),
65+
new Color(1, 0, 0).convert('hsb'), 'Color from hsb object literal');
6666

67-
compareRgbColors(new Color([1, 0, 0]), new Color(1, 0, 0),
68-
'RgbColor from array');
67+
compareColors(new Color([1, 0, 0]), new Color(1, 0, 0),
68+
'Rgb Color from array');
6969

70-
compareHsbColors(new HsbColor('#000000'), new HsbColor(0, 0, 0),
71-
'HsbColor from hex code');
72-
73-
compareHsbColors(new HsbColor({ red: 1, green: 0, blue: 0}),
74-
new HsbColor(0, 1, 1), 'HsbColor from rgb object literal');
75-
76-
compareHsbColors(new HsbColor({ gray: 0.8 }),
77-
new HsbColor(0, 0, 0.2), 'RgbColor from gray object literal');
78-
79-
compareHsbColors(new HsbColor([1, 0, 0]), new HsbColor(1, 0, 0),
80-
'HsbColor from array');
81-
82-
compareGrayColors(new GrayColor('#000000'), new GrayColor(1),
83-
'GrayColor from hex code');
84-
85-
compareGrayColors(new GrayColor('#ffffff'), new GrayColor(0),
86-
'GrayColor from hex code');
87-
88-
compareGrayColors(new GrayColor({ red: 1, green: 1, blue: 1}),
89-
new GrayColor(0), 'GrayColor from rgb object literal');
90-
91-
compareGrayColors(new GrayColor({ gray: 0.2 }),
92-
new GrayColor(0.2), 'GrayColor from gray object literal');
93-
94-
compareGrayColors(new GrayColor({ hue: 0, saturation: 0, brightness: 0.8}),
95-
new GrayColor(0.2), 'GrayColor from hsb object literal');
96-
97-
compareGrayColors(new GrayColor([1]), new GrayColor(1),
98-
'GrayColor from array');
70+
compareColors(new Color([1]), new Color(1),
71+
'Gray Color from array');
9972
});
10073

101-
test('Get gray from RgbColor', function() {
74+
test('Get gray from RGB Color', function() {
10275
var color = new Color(1, 0.5, 0.2);
10376
compareNumbers(color.gray, 0.38458251953125);
10477

10578
var color = new Color(0.5, 0.2, 0.1);
10679
compareNumbers(color.gray, 0.72137451171875);
10780
});
10881

109-
test('Get gray from HsbColor', function() {
82+
test('Get gray from HSB Color', function() {
11083
var color = new HsbColor(0, 0, 0.2);
11184
compareNumbers(color.gray, 0.8);
11285
});
11386

114-
test('Get red from HsbColor', function() {
87+
test('Get red from HSB Color', function() {
11588
var color = new HsbColor(0, 1, 1);
11689
compareNumbers(color.red, 1);
11790
});
11891

119-
test('Get hue from RgbColor', function() {
92+
test('Get hue from RGB Color', function() {
12093
var color = new Color(1, 0, 0);
12194
compareNumbers(color.hue, 0);
12295
compareNumbers(color.saturation, 1);
12396
});
12497

12598
test('Gray Color', function() {
126-
var color = new GrayColor(1);
99+
var color = new Color(1);
127100
compareNumbers(color.gray, 1);
128101
compareNumbers(color.red, 0);
102+
compareNumbers(color.blue, 0);
129103

130104
color.red = 0.5;
131-
compareNumbers(color.gray, '0.84999');
105+
compareNumbers(color.gray, 0.85055);
132106

133107
color.green = 0.2;
134-
compareNumbers(color.gray, '0.82051');
108+
109+
compareNumbers(color.red, 0.5);
110+
compareNumbers(color.green, 0.2);
111+
compareNumbers(color.blue, 0);
112+
113+
compareNumbers(color.gray, 0.73315);
135114
});
136115

137116
test('Converting Colors', function() {
138117
var rgbColor = new Color(1, 0.5, 0.2);
139-
compareNumbers(new GrayColor(rgbColor).gray, 0.38299560546875);
140-
141-
var grayColor = new GrayColor(0.2);
142-
var rgbColor = new Color(grayColor);
143-
compareRgbColors(rgbColor, [ 0.8, 0.8, 0.8, 1]);
144-
145-
var hsbColor = new HsbColor(grayColor);
146-
compareHsbColors(hsbColor, [ 0, 0, 0.8, 1]);
147-
148-
var rgbColor = new Color(1, 0, 0);
149-
compareHsbColors(new HsbColor(rgbColor), [0, 1, 1, 1]);
118+
compareNumbers(rgbColor.gray, 0.38299560546875);
119+
var grayColor = new Color(0.2);
120+
compareColors(grayColor.convert('rgb'), new Color(0.8, 0.8, 0.8));
121+
compareColors(grayColor.convert('hsb'), { hue: 0, saturation: 0, brightness: 0.8 });
122+
compareColors(new Color(1, 0, 0).convert('hsb'), { hue: 0, saturation: 1, brightness: 1 });
150123
});
151124

152-
test('Setting RgbColor#gray', function() {
125+
test('Setting Color#gray', function() {
153126
var color = new Color(1, 0.5, 0.2);
154127
color.gray = 0.1;
155-
compareRgbColors(color, [ 0.9, 0.9, 0.9, 1]);
128+
compareColors(color, new Color(0.1));
156129
});
157130

158-
test('Setting HsbColor#red', function() {
159-
var color = new HsbColor(180, 0, 0);
131+
test('Setting Color#red', function() {
132+
var color = new Color({ hue: 180, saturation: 0, brightness: 0 });
160133
color.red = 1;
161-
compareHsbColors(color, [0, 1, 1, 1]);
134+
compareColors(color, new Color(0, 1, 1));
162135
});
163136

164-
test('Setting HsbColor#gray', function() {
165-
var color = new HsbColor(180, 0, 0);
137+
test('Setting Color#gray', function() {
138+
var color = new Color({ hue: 180, saturation: 0, brightness: 0 });
166139
color.gray = 0.5;
167-
compareHsbColors(color, [0, 0, 0.5, 1]);
140+
compareColors(color, new Color(0.5));
168141
});
169142

170143
test('Color.read(channels)', function() {
171144
var color = Color.read([0, 0, 1]);
172-
compareRgbColors(color, [0, 0, 1, 1]);
145+
compareColors(color, new Color(0, 0, 1));
173146
});
174147

175148
test('Cloning colors', function() {
@@ -196,6 +169,6 @@ test('Color#convert', function() {
196169

197170
test('Saturation from black rgb', function() {
198171
equals(function() {
199-
return new Color(0, 0, 0).saturation == 0;
172+
return new Color(0, 0, 0).saturation === 0;
200173
}, true);
201174
});

test/tests/Item.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ test('Item#blendMode in a transformed Group', function() {
641641
});
642642

643643
var raster = layer.rasterize();
644-
compareRgbColors(raster.getPixel(0, 0), new Color(1, 0, 0),
644+
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0),
645645
'Top left pixel should be red:');
646-
compareRgbColors(raster.getPixel(50, 50), new Color(1, 1, 0),
646+
compareColors(raster.getPixel(50, 50), new Color(1, 1, 0),
647647
'Middle center pixel should be yellow:');
648648

649649
raster.remove();
@@ -653,8 +653,8 @@ test('Item#blendMode in a transformed Group', function() {
653653
group.position = [50, 50];
654654

655655
var raster = layer.rasterize();
656-
compareRgbColors(raster.getPixel(0, 0), new Color(1, 0, 0),
656+
compareColors(raster.getPixel(0, 0), new Color(1, 0, 0),
657657
'Top left pixel should be red:');
658-
compareRgbColors(raster.getPixel(50, 50), new Color(1, 1, 0),
658+
compareColors(raster.getPixel(50, 50), new Color(1, 1, 0),
659659
'Middle center pixel should be yellow:');
660660
});

0 commit comments

Comments
 (0)