1 if (typeof(dojo
) != 'undefined') { dojo
.require('MochiKit.Color'); }
2 if (typeof(JSAN
) != 'undefined') { JSAN
.use('MochiKit.Color'); }
3 if (typeof(tests
) == 'undefined') { tests
= {}; }
5 tests
.test_Color
= function (t
) {
6 var approx
= function (a
, b
, msg
) {
7 return t
.is(a
.toPrecision(4), b
.toPrecision(4), msg
);
10 t
.is( Color
.whiteColor().toHexString(), "#ffffff", "whiteColor has right hex" );
11 t
.is( Color
.blackColor().toHexString(), "#000000", "blackColor has right hex" );
12 t
.is( Color
.blueColor().toHexString(), "#0000ff", "blueColor has right hex" );
13 t
.is( Color
.redColor().toHexString(), "#ff0000", "redColor has right hex" );
14 t
.is( Color
.greenColor().toHexString(), "#00ff00", "greenColor has right hex" );
15 t
.is( compare(Color
.whiteColor(), Color
.whiteColor()), 0, "default colors compare right" );
16 t
.ok( Color
.whiteColor() == Color
.whiteColor(), "default colors are interned" );
17 t
.ok( Color
.whiteColor().toRGBString(), "rgb(255,255,255)", "toRGBString white" );
18 t
.ok( Color
.blueColor().toRGBString(), "rgb(0,0,255)", "toRGBString blue" );
19 t
.is( Color
.fromRGB(190/255, 222/255, 173/255).toHexString(), "#bedead", "fromRGB works" );
20 t
.is( Color
.fromRGB(226/255, 15.9/255, 182/255).toHexString(), "#e210b6", "fromRGB < 16 works" );
21 t
.is( Color
.fromRGB({r
:190/255,g:222/255,b
:173/255}).toHexString(), "#bedead", "alt fromRGB works" );
22 t
.is( Color
.fromHexString("#bedead").toHexString(), "#bedead", "round-trip hex" );
23 t
.is( Color
.fromString("#bedead").toHexString(), "#bedead", "round-trip string(hex)" );
24 t
.is( Color
.fromRGBString("rgb(190,222,173)").toHexString(), "#bedead", "round-trip rgb" );
25 t
.is( Color
.fromString("rgb(190,222,173)").toHexString(), "#bedead", "round-trip rgb" );
27 var hsl
= Color
.redColor().asHSL();
28 approx( hsl
.h
, 0.0, "red hsl.h" );
29 approx( hsl
.s
, 1.0, "red hsl.s" );
30 approx( hsl
.l
, 0.5, "red hsl.l" );
31 hsl
= Color
.fromRGB(0, 0, 0.5).asHSL();
32 approx( hsl
.h
, 2/3, "darkblue hsl.h" );
33 approx( hsl
.s
, 1.0, "darkblue hsl.s" );
34 approx( hsl
.l
, 0.25, "darkblue hsl.l" );
35 hsl
= Color
.fromString("#4169E1").asHSL();
36 approx( hsl
.h
, (5/8), "4169e1 h");
37 approx( hsl
.s
, (8/11), "4169e1 s");
38 approx( hsl
.l
, (29/51), "4169e1 l");
39 hsl
= Color
.fromString("#555544").asHSL();
40 approx( hsl
.h
, (1/6), "555544 h" );
41 approx( hsl
.s
, (1/9), "555544 s" );
42 approx( hsl
.l
, (3/10), "555544 l" );
43 hsl
= Color
.fromRGB(0.5, 1, 0.5).asHSL();
44 approx( hsl
.h
, 1/3, "aqua hsl.h" );
45 approx( hsl
.s
, 1.0, "aqua hsl.s" );
46 approx( hsl
.l
, 0.75, "aqua hsl.l" );
48 Color
.fromHSL(hsl
.h
, hsl
.s
, hsl
.l
).toHexString(),
49 Color
.fromRGB(0.5, 1, 0.5).toHexString(),
50 "fromHSL works with components"
53 Color
.fromHSL(hsl
).toHexString(),
54 Color
.fromRGB(0.5, 1, 0.5).toHexString(),
58 Color
.fromString("hsl(120,100%,75%)").toHexString(),
63 Color
.fromRGB(0.5, 1, 0.5).toHSLString(),
64 "hsl(120,100.0%,75.00%)",
67 t
.is( Color
.fromHSL(0, 0, 0).toHexString(), "#000000", "fromHSL to black" );
68 hsl
= Color
.blackColor().asHSL();
69 approx( hsl
.h
, 0.0, "black hsl.h" );
70 approx( hsl
.s
, 0.0, "black hsl.s" );
71 approx( hsl
.l
, 0.0, "black hsl.l" );
73 hsl
= Color
.blackColor().asHSL();
74 approx( hsl
.h
, 0.0, "asHSL returns copy" );
75 var rgb
= Color
.brownColor().asRGB();
76 approx( rgb
.r
, 153/255, "brown rgb.r" );
77 approx( rgb
.g
, 102/255, "brown rgb.g" );
78 approx( rgb
.b
, 51/255, "brown rgb.b" );
80 rgb
= Color
.brownColor().asRGB();
81 approx( rgb
.r
, 153/255, "asRGB returns copy" );
83 t
.is( Color
.fromName("aqua").toHexString(), "#00ffff", "aqua fromName" );
84 t
.is( Color
.fromString("aqua").toHexString(), "#00ffff", "aqua fromString" );
85 t
.is( Color
.fromName("transparent"), Color
.transparentColor(), "transparent fromName" );
86 t
.is( Color
.fromString("transparent"), Color
.transparentColor(), "transparent fromString" );
87 t
.is( Color
.transparentColor().toRGBString(), "rgba(0,0,0,0)", "transparent toRGBString" );
88 t
.is( Color
.fromRGBString("rgba( 0, 255, 255, 50%)").asRGB().a
, 0.5, "rgba parsing alpha correctly" );
89 t
.is( Color
.fromRGBString("rgba( 0, 255, 255, 50%)").toRGBString(), "rgba(0,255,255,0.5)", "rgba output correctly" );
90 t
.is( Color
.fromRGBString("rgba( 0, 255, 255, 1)").toHexString(), "#00ffff", "fromRGBString with spaces and alpha" );
91 t
.is( Color
.fromRGBString("rgb( 0, 255, 255)").toHexString(), "#00ffff", "fromRGBString with spaces" );
92 t
.is( Color
.fromRGBString("rgb( 0, 100%, 255)").toHexString(), "#00ffff", "fromRGBString with percents" );
94 var hsv
= Color
.redColor().asHSV();
95 approx( hsv
.h
, 0.0, "red hsv.h" );
96 approx( hsv
.s
, 1.0, "red hsv.s" );
97 approx( hsv
.v
, 1.0, "red hsv.v" );
98 t
.is( Color
.fromHSV(hsv
).toHexString(), Color
.redColor().toHexString(), "red hexstring" );
99 hsv
= Color
.fromRGB(0, 0, 0.5).asHSV();
100 approx( hsv
.h
, 2/3, "darkblue hsv.h" );
101 approx( hsv
.s
, 1.0, "darkblue hsv.s" );
102 approx( hsv
.v
, 0.5, "darkblue hsv.v" );
103 t
.is( Color
.fromHSV(hsv
).toHexString(), Color
.fromRGB(0, 0, 0.5).toHexString(), "darkblue hexstring" );
104 hsv
= Color
.fromString("#4169E1").asHSV();
105 approx( hsv
.h
, 5/8, "4169e1 h");
106 approx( hsv
.s
, 32/45, "4169e1 s");
107 approx( hsv
.v
, 15/17, "4169e1 l");
108 t
.is( Color
.fromHSV(hsv
).toHexString(), "#4169e1", "4169e1 hexstring" );
109 hsv
= Color
.fromString("#555544").asHSV();
110 approx( hsv
.h
, 1/6, "555544 h" );
111 approx( hsv
.s
, 1/5, "555544 s" );
112 approx( hsv
.v
, 1/3, "555544 l" );
113 t
.is( Color
.fromHSV(hsv
).toHexString(), "#555544", "555544 hexstring" );
114 hsv
= Color
.fromRGB(0.5, 1, 0.5).asHSV();
115 approx( hsv
.h
, 1/3, "aqua hsv.h" );
116 approx( hsv
.s
, 0.5, "aqua hsv.s" );
117 approx( hsv
.v
, 1, "aqua hsv.v" );
119 Color
.fromHSV(hsv
.h
, hsv
.s
, hsv
.v
).toHexString(),
120 Color
.fromRGB(0.5, 1, 0.5).toHexString(),
121 "fromHSV works with components"
124 Color
.fromHSV(hsv
).toHexString(),
125 Color
.fromRGB(0.5, 1, 0.5).toHexString(),
128 hsv
= Color
.fromRGB(1, 1, 1).asHSV()
129 approx( hsv
.h
, 0, 'white hsv.h' );
130 approx( hsv
.s
, 0, 'white hsv.s' );
131 approx( hsv
.v
, 1, 'white hsv.v' );
133 Color
.fromHSV(0, 0, 1).toHexString(),