7c70d073a638d68e2bac3f726c8404452ec3ef0c
[PlinnDocument.git] / skins / color_utils.js
1 // (c) Benoît PIN 2006-2007
2 // http://plinn.org
3 // Licence GPL
4
5 function HSVColor(h, s, v) {
6 this.h = h; // 0-360°
7 this.s = s; // 0-100%
8 this.v = v; // 0-100%
9 }
10
11 HSVColor.prototype.toRGBString = function() {
12 var rgb = this.toRGB();
13 return "rgb(" + String(rgb[0]) + ", " + String(rgb[1]) + ", " + String(rgb[2]) + ")";
14 };
15
16 HSVColor.prototype.toRGB = function () {
17 var h = this.h; var s = this.s/100.0;
18 var v = this.v/100.0;
19 if( s == 0 ) // achromatic (grey)
20 r = g = b = v;
21
22 h = h/60; // sector 0 to 5
23 var i = Math.floor( h );
24 var f = h - i; // factorial part of h
25 var p = v * ( 1 - s );
26 var q = v * ( 1 - s * f );
27 var t = v * ( 1 - s * ( 1 - f ) );
28 switch( i ) {
29 case 0:
30 r = v;
31 g = t;
32 b = p;
33 break;
34 case 1:
35 r = q;
36 g = v;
37 b = p;
38 break;
39 case 2:
40 r = p;
41 g = v;
42 b = t;
43 break;
44 case 3:
45 r = p;
46 g = q;
47 b = v;
48 break;
49 case 4:
50 r = t;
51 g = p;
52 b = v;
53 break;
54 default: // case 5:
55 r = v;
56 g = p;
57 b = q;
58 break;
59 }
60
61 r = Math.round(r*255);
62 g = Math.round(g*255);
63 b = Math.round(b*255);
64 return [r, g, b];
65 };
66
67
68 function ColorIterator(baseH, baseS, baseV, step) {
69 this.baseColor = new HSVColor(baseH, baseS, baseV);
70 this.currentH = baseH;
71 this.step = step;
72 }
73
74 ColorIterator.prototype.next = function() {
75 var c = new HSVColor(this.currentH, this.baseColor.s, this.baseColor.v);
76 this.currentH += this.step;
77
78 if (this.currentH >= 360)
79 this.currentH -= 360;
80
81 return c;
82 };
83
84 function setBorderColor(ob, colorString) {
85 var s = ob.style;
86 s.borderBottomColor = colorString;
87 s.borderLeftColor = colorString;
88 s.borderRightColor = colorString;
89 s.borderTopColor = colorString;
90 }
91
92
93 function setBorderStyle(ob, borderStyle) {
94 var s= ob.style;
95 s.borderBottomStyle = borderStyle;
96 s.borderLeftStyle = borderStyle;
97 s.borderRightStyle = borderStyle;
98 s.borderTopStyle = borderStyle;
99 }
100
101
102 function setBorderWidth(ob, borderWidth) {
103 var s= ob.style;
104 s.borderBottomWidth = borderWidth;
105 s.borderLeftWidth = borderWidth;
106 s.borderRightWidth = borderWidth;
107 s.borderTopWidth = borderWidth;
108 }
109
110 function zfill(number, digits) {
111 number = Math.floor(number);
112 var nStr = String(number);
113 while (nStr.length < digits)
114 nStr = "0" + nStr;
115 return nStr;
116 }
117