1 description("Test of various canvas graphics context calls for setting colors.");
3 var canvas = document.createElement("canvas");
4 var context = canvas.getContext('2d');
8 context.clearRect(0, 0, canvas.width, canvas.height);
13 var hexDigits = "0123456789abcdef";
14 return hexDigits[number >> 4] + hexDigits[number & 0xF];
19 var imageData = context.getImageData(0, 0, 1, 1);
20 if (imageData.data[3] == 255)
21 return "#" + hex(imageData.data[0]) + hex(imageData.data[1]) + hex(imageData.data[2]);
22 if (imageData.data[3] == 0)
23 return "rgba(" + imageData.data[0] + ", " + imageData.data[1] + ", " + imageData.data[2] + ", 0.0)";
24 return "rgba(" + imageData.data[0] + ", " + imageData.data[1] + ", " + imageData.data[2] + ", " + (imageData.data[3] / 255) + ")";
27 function testFillStyle(string)
30 context.fillStyle = "black";
31 context.fillStyle = string;
32 context.fillRect(0, 0, 1, 1);
36 function testFillGradient(string)
39 context.fillStyle = "black";
40 var gradient = context.createLinearGradient(0, 0, 1, 1);
41 gradient.addColorStop(0, string);
42 gradient.addColorStop(1, string);
43 context.fillStyle = gradient;
44 context.fillRect(0, 0, 1, 1);
48 function testSetFillColor(arguments)
51 context.fillStyle = "black";
52 eval("context.setFillColor(" + arguments + ")");
53 context.fillRect(0, 0, 1, 1);
57 function testStrokeStyle(string)
60 context.lineWidth = 5;
61 context.strokeStyle = "black";
62 context.strokeStyle = string;
63 context.strokeRect(2, 2, 10, 10);
67 function testStrokeGradient(string)
70 context.lineWidth = 5;
71 context.strokeStyle = "black";
72 var gradient = context.createLinearGradient(0, 0, 1, 1);
73 gradient.addColorStop(0, string);
74 gradient.addColorStop(1, string);
75 context.strokeStyle = gradient;
76 context.strokeRect(2, 2, 10, 10);
80 function testSetStrokeColor(arguments)
83 context.lineWidth = 5;
84 context.strokeStyle = "black";
85 eval("context.setStrokeColor(" + arguments + ")");
86 context.strokeRect(2, 2, 10, 10);
90 var transparent = "rgba(0, 0, 0, 0.0)";
92 var green = "#00ff00";
94 var white = "#ffffff";
95 var translucentRed = "rgba(255, 0, 0, 0.8)";
96 var translucentGreen = "rgba(0, 255, 0, 0.8)";
97 var translucentBlue = "rgba(0, 0, 255, 0.8)";
98 var translucentWhite = "rgba(255, 255, 255, 0.8)";
100 shouldBe("testFillStyle('transparent')", "transparent");
101 shouldBe("testFillStyle('blue')", "blue");
102 shouldBe("testFillStyle('#FF0000')", "red");
103 shouldBe("testFillStyle('#f00')", "red");
104 shouldBe("testFillStyle('rgb(255, 0, 0)')", "red");
105 shouldBe("testFillStyle('rgba(255, 0, 0, 1)')", "red");
106 shouldBe("testFillStyle('rgba(255, 0, 0, 0.8)')", "translucentRed");
107 shouldBe("testFillStyle('rgba(255, 0, 0, 0)')", "transparent");
108 shouldBe("testFillGradient('transparent')", "transparent");
109 shouldBe("testFillGradient('blue')", "blue");
110 shouldBe("testFillGradient('#FF0000')", "red");
111 shouldBe("testFillGradient('#f00')", "red");
112 shouldBe("testFillGradient('rgb(255, 0, 0)')", "red");
113 shouldBe("testFillGradient('rgba(255, 0, 0, 1)')", "red");
114 shouldBe("testFillGradient('rgba(255, 0, 0, 0.8)')", "translucentRed");
115 shouldBe("testFillGradient('rgba(255, 0, 0, 0)')", "transparent");
116 shouldBe("testSetFillColor('\"blue\"')", "blue");
117 shouldBe("testSetFillColor('\"#FF0000\"')", "red");
118 shouldBe("testSetFillColor('\"#f00\"')", "red");
119 shouldBe("testSetFillColor('\"rgb(255, 0, 0)\"')", "red");
120 shouldBe("testSetFillColor('\"rgba(255, 0, 0, 1)\"')", "red");
121 shouldBe("testSetFillColor('\"rgba(255, 0, 0, 0.8)\"')", "translucentRed");
122 shouldBe("testSetFillColor('\"rgba(255, 0, 0, 0)\"')", "transparent");
123 shouldBe("testSetFillColor('\"blue\", 0.8')", "translucentBlue");
124 shouldBe("testSetFillColor('1')", "white");
125 shouldBe("testSetFillColor('1, 0.8')", "translucentWhite");
126 shouldBe("testSetFillColor('0, 1, 0, 1')", "green");
127 shouldBe("testSetFillColor('0, 1, 0, 0.8')", "translucentGreen");
128 shouldBe("testSetFillColor('0, 1, 0, 0, 1')", "'#c9006c'");
129 shouldBe("testSetFillColor('0, 1, 0, 0, 0.8')", "'rgba(200, 0, 107, 0.8)'");
130 shouldBe("testSetFillColor('0, 1, 0, 0, 0')", "transparent");
131 shouldBe("testStrokeStyle('transparent')", "transparent");
132 shouldBe("testStrokeStyle('blue')", "blue");
133 shouldBe("testStrokeStyle('#FF0000')", "red");
134 shouldBe("testStrokeStyle('#f00')", "red");
135 shouldBe("testStrokeStyle('rgb(255, 0, 0)')", "red");
136 shouldBe("testStrokeStyle('rgba(255, 0, 0, 1)')", "red");
137 shouldBe("testStrokeStyle('rgba(255, 0, 0, 0.8)')", "translucentRed");
138 shouldBe("testStrokeStyle('rgba(255, 0, 0, 0)')", "transparent");
139 shouldBe("testStrokeGradient('transparent')", "transparent");
140 shouldBe("testStrokeGradient('blue')", "blue");
141 shouldBe("testStrokeGradient('#FF0000')", "red");
142 shouldBe("testStrokeGradient('#f00')", "red");
143 shouldBe("testStrokeGradient('rgb(255, 0, 0)')", "red");
144 shouldBe("testStrokeGradient('rgba(255, 0, 0, 1)')", "red");
145 shouldBe("testStrokeGradient('rgba(255, 0, 0, 0.8)')", "translucentRed");
146 shouldBe("testStrokeGradient('rgba(255, 0, 0, 0)')", "transparent");
147 shouldBe("testSetStrokeColor('\"blue\"')", "blue");
148 shouldBe("testSetStrokeColor('\"#FF0000\"')", "red");
149 shouldBe("testSetStrokeColor('\"#f00\"')", "red");
150 shouldBe("testSetStrokeColor('\"rgb(255, 0, 0)\"')", "red");
151 shouldBe("testSetStrokeColor('\"rgba(255, 0, 0, 1)\"')", "red");
152 shouldBe("testSetStrokeColor('\"rgba(255, 0, 0, 0.8)\"')", "translucentRed");
153 shouldBe("testSetStrokeColor('\"rgba(255, 0, 0, 0)\"')", "transparent");
154 shouldBe("testSetStrokeColor('\"blue\", 0.8')", "translucentBlue");
155 shouldBe("testSetStrokeColor('1')", "white");
156 shouldBe("testSetStrokeColor('1, 0.8')", "translucentWhite");
157 shouldBe("testSetStrokeColor('0, 1, 0, 1')", "green");
158 shouldBe("testSetStrokeColor('0, 1, 0, 0.8')", "translucentGreen");
159 shouldBe("testSetStrokeColor('0, 1, 0, 0, 1')", "'#c9006c'");
160 shouldBe("testSetStrokeColor('0, 1, 0, 0, 0.8')", "'rgba(200, 0, 107, 0.8)'");
161 shouldBe("testSetStrokeColor('0, 1, 0, 0, 0')", "transparent");
163 var successfullyParsed = true;