]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/Color.js.uncompressed.js
dba93b8f425013389133d202966bc14d93784913
1 define("dojo/_base/Color", ["./kernel", "./lang", "./array", "./config"], function(dojo
, lang
, ArrayUtil
, config
){
3 var Color
= dojo
.Color = function(/*Array|String|Object*/ color
){
5 // Takes a named string, hex string, array of rgb or rgba values,
6 // an object with r, g, b, and a properties, or another `Color` object
7 // and creates a new Color instance to work from.
10 // Work with a Color instance:
11 // | var c = new Color();
12 // | c.setColor([0,0,0]); // black
13 // | var hex = c.toHex(); // #000000
16 // Work with a node's color:
17 // | var color = dojo.style("someNode", "backgroundColor");
18 // | var n = new Color(color);
19 // | // adjust the color some
21 // | console.log(n.toString()); // rgb(128, 255, 255);
22 if(color
){ this.setColor(color
); }
26 // there's got to be a more space-efficient way to encode or discover
30 // Dictionary list of all CSS named colors, by name. Values are 3-item arrays with corresponding RG and B values.
32 "silver": [192,192,192],
33 "gray": [128,128,128],
34 "white": [255,255,255],
37 "purple": [128,0,128],
38 "fuchsia":[255,0,255],
42 "yellow": [255,255,0],
47 "transparent": config
.transparentColor
|| [0,0,0,0]
51 r
: 255, g
: 255, b
: 255, a
: 1,
52 _set: function(r
, g
, b
, a
){
53 var t
= this; t
.r
= r
; t
.g
= g
; t
.b
= b
; t
.a
= a
;
55 setColor: function(/*Array|String|Object*/ color
){
57 // Takes a named string, hex string, array of rgb or rgba values,
58 // an object with r, g, b, and a properties, or another `Color` object
59 // and sets this color instance to that value.
62 // | var c = new Color(); // no color
63 // | c.setColor("#ededed"); // greyish
64 if(lang
.isString(color
)){
65 Color
.fromString(color
, this);
66 }else if(lang
.isArray(color
)){
67 Color
.fromArray(color
, this);
69 this._set(color
.r
, color
.g
, color
.b
, color
.a
);
70 if(!(color
instanceof Color
)){ this.sanitize(); }
76 // Ensures the object has correct attributes
78 // the default implementation does nothing, include dojo.colors to
79 // augment it with real checks
84 // Returns 3 component array of rgb values
86 // | var c = new Color("#000000");
87 // | console.log(c.toRgb()); // [0,0,0]
89 return [t
.r
, t
.g
, t
.b
]; // Array
93 // Returns a 4 component array of rgba values from the color
94 // represented by this object.
96 return [t
.r
, t
.g
, t
.b
, t
.a
]; // Array
100 // Returns a CSS color string in hexadecimal representation
102 // | console.log(new Color([0,0,0]).toHex()); // #000000
103 var arr
= ArrayUtil
.map(["r", "g", "b"], function(x
){
104 var s
= this[x
].toString(16);
105 return s
.length
< 2 ? "0" + s
: s
;
107 return "#" + arr
.join(""); // String
109 toCss: function(/*Boolean?*/ includeAlpha
){
111 // Returns a css color string in rgb(a) representation
113 // | var c = new Color("#FFF").toCss();
114 // | console.log(c); // rgb('255','255','255')
115 var t
= this, rgb
= t
.r
+ ", " + t
.g
+ ", " + t
.b
;
116 return (includeAlpha
? "rgba(" + rgb
+ ", " + t
.a
: "rgb(" + rgb
) + ")"; // String
118 toString: function(){
120 // Returns a visual representation of the color
121 return this.toCss(true); // String
125 Color
.blendColors
= dojo
.blendColors = function(
132 // Blend colors end and start with weight from 0 to 1, 0.5 being a 50/50 blend,
133 // can reuse a previously allocated Color object for the result
134 var t
= obj
|| new Color();
135 ArrayUtil
.forEach(["r", "g", "b", "a"], function(x
){
136 t
[x
] = start
[x
] + (end
[x
] - start
[x
]) * weight
;
137 if(x
!= "a"){ t
[x
] = Math
.round(t
[x
]); }
139 return t
.sanitize(); // Color
142 Color
.fromRgb
= dojo
.colorFromRgb = function(/*String*/ color
, /*Color?*/ obj
){
144 // Returns a `Color` instance from a string of the form
145 // "rgb(...)" or "rgba(...)". Optionally accepts a `Color`
146 // object to update with the parsed value and return instead of
147 // creating a new object.
149 // A Color object. If obj is passed, it will be the return value.
150 var m
= color
.toLowerCase().match(/^rgba?\(([\s\.,0-9]+)\)/);
151 return m
&& Color
.fromArray(m
[1].split(/\s*,\s*/), obj
); // Color
154 Color
.fromHex
= dojo
.colorFromHex = function(/*String*/ color
, /*Color?*/ obj
){
156 // Converts a hex string with a '#' prefix to a color object.
157 // Supports 12-bit #rgb shorthand. Optionally accepts a
158 // `Color` object to update with the parsed value.
161 // A Color object. If obj is passed, it will be the return value.
164 // | var thing = dojo.colorFromHex("#ededed"); // grey, longhand
167 // | var thing = dojo.colorFromHex("#000"); // black, shorthand
168 var t
= obj
|| new Color(),
169 bits
= (color
.length
== 4) ? 4 : 8,
170 mask
= (1 << bits
) - 1;
171 color
= Number("0x" + color
.substr(1));
173 return null; // Color
175 ArrayUtil
.forEach(["b", "g", "r"], function(x
){
176 var c
= color
& mask
;
178 t
[x
] = bits
== 4 ? 17 * c
: c
;
184 Color
.fromArray
= dojo
.colorFromArray = function(/*Array*/ a
, /*Color?*/ obj
){
186 // Builds a `Color` from a 3 or 4 element array, mapping each
187 // element in sequence to the rgb(a) values of the color.
189 // | var myColor = dojo.colorFromArray([237,237,237,0.5]); // grey, 50% alpha
191 // A Color object. If obj is passed, it will be the return value.
192 var t
= obj
|| new Color();
193 t
._set(Number(a
[0]), Number(a
[1]), Number(a
[2]), Number(a
[3]));
194 if(isNaN(t
.a
)){ t
.a
= 1; }
195 return t
.sanitize(); // Color
198 Color
.fromString
= dojo
.colorFromString = function(/*String*/ str
, /*Color?*/ obj
){
200 // Parses `str` for a color value. Accepts hex, rgb, and rgba
201 // style color values.
203 // Acceptable input values for str may include arrays of any form
204 // accepted by dojo.colorFromArray, hex strings such as "#aaaaaa", or
205 // rgb or rgba strings such as "rgb(133, 200, 16)" or "rgba(10, 10,
208 // A Color object. If obj is passed, it will be the return value.
209 var a
= Color
.named
[str
];
210 return a
&& Color
.fromArray(a
, obj
) || Color
.fromRgb(str
, obj
) || Color
.fromHex(str
, obj
); // Color