]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/_base/Color.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / _base / Color.js
CommitLineData
2f01fe57
AD
1/*
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5*/
6
7
a089699c
AD
8if(!dojo._hasResource["dojo._base.Color"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9dojo._hasResource["dojo._base.Color"] = true;
2f01fe57
AD
10dojo.provide("dojo._base.Color");
11dojo.require("dojo._base.array");
12dojo.require("dojo._base.lang");
a089699c 13
2f01fe57 14(function(){
a089699c
AD
15
16 var d = dojo;
17
18 dojo.Color = function(/*Array|String|Object*/ color){
19 // summary:
20 // Takes a named string, hex string, array of rgb or rgba values,
21 // an object with r, g, b, and a properties, or another `dojo.Color` object
22 // and creates a new Color instance to work from.
23 //
24 // example:
25 // Work with a Color instance:
26 // | var c = new dojo.Color();
27 // | c.setColor([0,0,0]); // black
28 // | var hex = c.toHex(); // #000000
29 //
30 // example:
31 // Work with a node's color:
32 // | var color = dojo.style("someNode", "backgroundColor");
33 // | var n = new dojo.Color(color);
34 // | // adjust the color some
35 // | n.r *= .5;
36 // | console.log(n.toString()); // rgb(128, 255, 255);
37 if(color){ this.setColor(color); }
38 };
39
40 // FIXME:
41 // there's got to be a more space-efficient way to encode or discover
42 // these!! Use hex?
43 dojo.Color.named = {
44 black: [0,0,0],
45 silver: [192,192,192],
46 gray: [128,128,128],
47 white: [255,255,255],
48 maroon: [128,0,0],
49 red: [255,0,0],
50 purple: [128,0,128],
51 fuchsia: [255,0,255],
52 green: [0,128,0],
53 lime: [0,255,0],
54 olive: [128,128,0],
55 yellow: [255,255,0],
56 navy: [0,0,128],
57 blue: [0,0,255],
58 teal: [0,128,128],
59 aqua: [0,255,255],
60 transparent: d.config.transparentColor || [255,255,255]
61 };
62
63 dojo.extend(dojo.Color, {
64 r: 255, g: 255, b: 255, a: 1,
65 _set: function(r, g, b, a){
66 var t = this; t.r = r; t.g = g; t.b = b; t.a = a;
67 },
68 setColor: function(/*Array|String|Object*/ color){
69 // summary:
70 // Takes a named string, hex string, array of rgb or rgba values,
71 // an object with r, g, b, and a properties, or another `dojo.Color` object
72 // and sets this color instance to that value.
73 //
74 // example:
75 // | var c = new dojo.Color(); // no color
76 // | c.setColor("#ededed"); // greyish
77 if(d.isString(color)){
78 d.colorFromString(color, this);
79 }else if(d.isArray(color)){
80 d.colorFromArray(color, this);
81 }else{
82 this._set(color.r, color.g, color.b, color.a);
83 if(!(color instanceof d.Color)){ this.sanitize(); }
84 }
85 return this; // dojo.Color
86 },
87 sanitize: function(){
88 // summary:
89 // Ensures the object has correct attributes
90 // description:
91 // the default implementation does nothing, include dojo.colors to
92 // augment it with real checks
93 return this; // dojo.Color
94 },
95 toRgb: function(){
96 // summary:
97 // Returns 3 component array of rgb values
98 // example:
99 // | var c = new dojo.Color("#000000");
100 // | console.log(c.toRgb()); // [0,0,0]
101 var t = this;
102 return [t.r, t.g, t.b]; // Array
103 },
104 toRgba: function(){
105 // summary:
106 // Returns a 4 component array of rgba values from the color
107 // represented by this object.
108 var t = this;
109 return [t.r, t.g, t.b, t.a]; // Array
110 },
111 toHex: function(){
112 // summary:
113 // Returns a CSS color string in hexadecimal representation
114 // example:
115 // | console.log(new dojo.Color([0,0,0]).toHex()); // #000000
116 var arr = d.map(["r", "g", "b"], function(x){
117 var s = this[x].toString(16);
118 return s.length < 2 ? "0" + s : s;
119 }, this);
120 return "#" + arr.join(""); // String
121 },
122 toCss: function(/*Boolean?*/ includeAlpha){
123 // summary:
124 // Returns a css color string in rgb(a) representation
125 // example:
126 // | var c = new dojo.Color("#FFF").toCss();
127 // | console.log(c); // rgb('255','255','255')
128 var t = this, rgb = t.r + ", " + t.g + ", " + t.b;
129 return (includeAlpha ? "rgba(" + rgb + ", " + t.a : "rgb(" + rgb) + ")"; // String
130 },
131 toString: function(){
132 // summary:
133 // Returns a visual representation of the color
134 return this.toCss(true); // String
135 }
136 });
137
138 dojo.blendColors = function(
139 /*dojo.Color*/ start,
140 /*dojo.Color*/ end,
141 /*Number*/ weight,
142 /*dojo.Color?*/ obj
143 ){
144 // summary:
145 // Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend,
146 // can reuse a previously allocated dojo.Color object for the result
147 var t = obj || new d.Color();
148 d.forEach(["r", "g", "b", "a"], function(x){
149 t[x] = start[x] + (end[x] - start[x]) * weight;
150 if(x != "a"){ t[x] = Math.round(t[x]); }
151 });
152 return t.sanitize(); // dojo.Color
153 };
154
155 dojo.colorFromRgb = function(/*String*/ color, /*dojo.Color?*/ obj){
156 // summary:
157 // Returns a `dojo.Color` instance from a string of the form
158 // "rgb(...)" or "rgba(...)". Optionally accepts a `dojo.Color`
159 // object to update with the parsed value and return instead of
160 // creating a new object.
161 // returns:
162 // A dojo.Color object. If obj is passed, it will be the return value.
163 var m = color.toLowerCase().match(/^rgba?\(([\s\.,0-9]+)\)/);
164 return m && dojo.colorFromArray(m[1].split(/\s*,\s*/), obj); // dojo.Color
165 };
166
167 dojo.colorFromHex = function(/*String*/ color, /*dojo.Color?*/ obj){
168 // summary:
169 // Converts a hex string with a '#' prefix to a color object.
170 // Supports 12-bit #rgb shorthand. Optionally accepts a
171 // `dojo.Color` object to update with the parsed value.
172 //
173 // returns:
174 // A dojo.Color object. If obj is passed, it will be the return value.
175 //
176 // example:
177 // | var thing = dojo.colorFromHex("#ededed"); // grey, longhand
178 //
179 // example:
180 // | var thing = dojo.colorFromHex("#000"); // black, shorthand
181 var t = obj || new d.Color(),
182 bits = (color.length == 4) ? 4 : 8,
183 mask = (1 << bits) - 1;
184 color = Number("0x" + color.substr(1));
185 if(isNaN(color)){
186 return null; // dojo.Color
187 }
188 d.forEach(["b", "g", "r"], function(x){
189 var c = color & mask;
190 color >>= bits;
191 t[x] = bits == 4 ? 17 * c : c;
192 });
193 t.a = 1;
194 return t; // dojo.Color
195 };
196
197 dojo.colorFromArray = function(/*Array*/ a, /*dojo.Color?*/ obj){
198 // summary:
199 // Builds a `dojo.Color` from a 3 or 4 element array, mapping each
200 // element in sequence to the rgb(a) values of the color.
201 // example:
202 // | var myColor = dojo.colorFromArray([237,237,237,0.5]); // grey, 50% alpha
203 // returns:
204 // A dojo.Color object. If obj is passed, it will be the return value.
205 var t = obj || new d.Color();
206 t._set(Number(a[0]), Number(a[1]), Number(a[2]), Number(a[3]));
207 if(isNaN(t.a)){ t.a = 1; }
208 return t.sanitize(); // dojo.Color
209 };
210
211 dojo.colorFromString = function(/*String*/ str, /*dojo.Color?*/ obj){
212 // summary:
213 // Parses `str` for a color value. Accepts hex, rgb, and rgba
214 // style color values.
215 // description:
216 // Acceptable input values for str may include arrays of any form
217 // accepted by dojo.colorFromArray, hex strings such as "#aaaaaa", or
218 // rgb or rgba strings such as "rgb(133, 200, 16)" or "rgba(10, 10,
219 // 10, 50)"
220 // returns:
221 // A dojo.Color object. If obj is passed, it will be the return value.
222 var a = d.Color.named[str];
223 return a && d.colorFromArray(a, obj) || d.colorFromRgb(str, obj) || d.colorFromHex(str, obj);
224 };
2f01fe57 225})();
a089699c 226
2f01fe57 227}