]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/Color.js
2 Copyright (c) 2004-2011, 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
8 if(!dojo
._hasResource
["dojo._base.Color"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo._base.Color"] = true;
10 dojo
.provide("dojo._base.Color");
11 dojo
.require("dojo._base.array");
12 dojo
.require("dojo._base.lang");
19 dojo
.Color = function(/*Array|String|Object*/ color
){
21 // Takes a named string, hex string, array of rgb or rgba values,
22 // an object with r, g, b, and a properties, or another `dojo.Color` object
23 // and creates a new Color instance to work from.
26 // Work with a Color instance:
27 // | var c = new dojo.Color();
28 // | c.setColor([0,0,0]); // black
29 // | var hex = c.toHex(); // #000000
32 // Work with a node's color:
33 // | var color = dojo.style("someNode", "backgroundColor");
34 // | var n = new dojo.Color(color);
35 // | // adjust the color some
37 // | console.log(n.toString()); // rgb(128, 255, 255);
38 if(color
){ this.setColor(color
); }
42 // there's got to be a more space-efficient way to encode or discover
46 silver
: [192,192,192],
61 transparent
: d
.config
.transparentColor
|| [255,255,255]
64 dojo
.extend(dojo
.Color
, {
65 r
: 255, g
: 255, b
: 255, a
: 1,
66 _set: function(r
, g
, b
, a
){
67 var t
= this; t
.r
= r
; t
.g
= g
; t
.b
= b
; t
.a
= a
;
69 setColor: function(/*Array|String|Object*/ color
){
71 // Takes a named string, hex string, array of rgb or rgba values,
72 // an object with r, g, b, and a properties, or another `dojo.Color` object
73 // and sets this color instance to that value.
76 // | var c = new dojo.Color(); // no color
77 // | c.setColor("#ededed"); // greyish
78 if(d
.isString(color
)){
79 d
.colorFromString(color
, this);
80 }else if(d
.isArray(color
)){
81 d
.colorFromArray(color
, this);
83 this._set(color
.r
, color
.g
, color
.b
, color
.a
);
84 if(!(color
instanceof d
.Color
)){ this.sanitize(); }
86 return this; // dojo.Color
90 // Ensures the object has correct attributes
92 // the default implementation does nothing, include dojo.colors to
93 // augment it with real checks
94 return this; // dojo.Color
98 // Returns 3 component array of rgb values
100 // | var c = new dojo.Color("#000000");
101 // | console.log(c.toRgb()); // [0,0,0]
103 return [t
.r
, t
.g
, t
.b
]; // Array
107 // Returns a 4 component array of rgba values from the color
108 // represented by this object.
110 return [t
.r
, t
.g
, t
.b
, t
.a
]; // Array
114 // Returns a CSS color string in hexadecimal representation
116 // | console.log(new dojo.Color([0,0,0]).toHex()); // #000000
117 var arr
= d
.map(["r", "g", "b"], function(x
){
118 var s
= this[x
].toString(16);
119 return s
.length
< 2 ? "0" + s
: s
;
121 return "#" + arr
.join(""); // String
123 toCss: function(/*Boolean?*/ includeAlpha
){
125 // Returns a css color string in rgb(a) representation
127 // | var c = new dojo.Color("#FFF").toCss();
128 // | console.log(c); // rgb('255','255','255')
129 var t
= this, rgb
= t
.r
+ ", " + t
.g
+ ", " + t
.b
;
130 return (includeAlpha
? "rgba(" + rgb
+ ", " + t
.a
: "rgb(" + rgb
) + ")"; // String
132 toString: function(){
134 // Returns a visual representation of the color
135 return this.toCss(true); // String
139 dojo
.blendColors = function(
140 /*dojo.Color*/ start
,
146 // Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend,
147 // can reuse a previously allocated dojo.Color object for the result
148 var t
= obj
|| new d
.Color();
149 d
.forEach(["r", "g", "b", "a"], function(x
){
150 t
[x
] = start
[x
] + (end
[x
] - start
[x
]) * weight
;
151 if(x
!= "a"){ t
[x
] = Math
.round(t
[x
]); }
153 return t
.sanitize(); // dojo.Color
156 dojo
.colorFromRgb = function(/*String*/ color
, /*dojo.Color?*/ obj
){
158 // Returns a `dojo.Color` instance from a string of the form
159 // "rgb(...)" or "rgba(...)". Optionally accepts a `dojo.Color`
160 // object to update with the parsed value and return instead of
161 // creating a new object.
163 // A dojo.Color object. If obj is passed, it will be the return value.
164 var m
= color
.toLowerCase().match(/^rgba?\(([\s\.,0-9]+)\)/);
165 return m
&& dojo
.colorFromArray(m
[1].split(/\s*,\s*/), obj
); // dojo.Color
168 dojo
.colorFromHex = function(/*String*/ color
, /*dojo.Color?*/ obj
){
170 // Converts a hex string with a '#' prefix to a color object.
171 // Supports 12-bit #rgb shorthand. Optionally accepts a
172 // `dojo.Color` object to update with the parsed value.
175 // A dojo.Color object. If obj is passed, it will be the return value.
178 // | var thing = dojo.colorFromHex("#ededed"); // grey, longhand
181 // | var thing = dojo.colorFromHex("#000"); // black, shorthand
182 var t
= obj
|| new d
.Color(),
183 bits
= (color
.length
== 4) ? 4 : 8,
184 mask
= (1 << bits
) - 1;
185 color
= Number("0x" + color
.substr(1));
187 return null; // dojo.Color
189 d
.forEach(["b", "g", "r"], function(x
){
190 var c
= color
& mask
;
192 t
[x
] = bits
== 4 ? 17 * c
: c
;
195 return t
; // dojo.Color
198 dojo
.colorFromArray = function(/*Array*/ a
, /*dojo.Color?*/ obj
){
200 // Builds a `dojo.Color` from a 3 or 4 element array, mapping each
201 // element in sequence to the rgb(a) values of the color.
203 // | var myColor = dojo.colorFromArray([237,237,237,0.5]); // grey, 50% alpha
205 // A dojo.Color object. If obj is passed, it will be the return value.
206 var t
= obj
|| new d
.Color();
207 t
._set(Number(a
[0]), Number(a
[1]), Number(a
[2]), Number(a
[3]));
208 if(isNaN(t
.a
)){ t
.a
= 1; }
209 return t
.sanitize(); // dojo.Color
212 dojo
.colorFromString = function(/*String*/ str
, /*dojo.Color?*/ obj
){
214 // Parses `str` for a color value. Accepts hex, rgb, and rgba
215 // style color values.
217 // Acceptable input values for str may include arrays of any form
218 // accepted by dojo.colorFromArray, hex strings such as "#aaaaaa", or
219 // rgb or rgba strings such as "rgb(133, 200, 16)" or "rgba(10, 10,
222 // A dojo.Color object. If obj is passed, it will be the return value.
223 var a
= d
.Color
.named
[str
];
224 return a
&& d
.colorFromArray(a
, obj
) || d
.colorFromRgb(str
, obj
) || d
.colorFromHex(str
, obj
);