This utility provides you with the ability to reliably convert between color formats, such as between between rgb and hex or hsv and rgb.
In order to keep the number of possible permutations of conversion methods to a minimum, rgb is assumed as the underlying type, so if you convert a format to rgb, you can convert it to any other format.
To view all available methods in the Alloy Color utility, please view the documentation.
YUI().use('gallery-aui-color-util', function(Y) {
//To retrieve any RGB value based on best guess input, such as keyword, hex code, rgb, rgba, hsb, hsl, hsla, etc, use getRGB, which returns an RGB object.
Y.ColorUtil.getRGB('cyan'); // returns {r: 0, g: 255, b: 255, hex: '#00ffff' }
Y.ColorUtil.getRGB('#f00'); // returns {r: 255, g: 0, b: 0, hex: '#ff0000' }
Y.ColorUtil.getRGB('rgba(25,20,58,0.5)'); // returns {r: 25, g: 20, b: 58, o: 0.5, hex: '#19143a' }
// To convert colors you can do any of the following:
Y.ColorUtil.hex2rgb('#fff'); // returns {r: 255, g: 255, b: 255, hex: '#ffffff'}
var yellow = Y.ColorUtil.getRGB('yellow'); // returns {r: 255, g: 255, b: 0, hex: '#ffff00' }
var hsv = Y.ColorUtil.rgb2hsv(yellow); // returns {h: 0.16666666666666666, s: 1, v: 1}
Y.ColorUtil.hsv2rgb(hsv); // returns {r: 255, g: 255, b: 0, hex: '#ffff00'}
// (all methods that can take multiple arguments can take either an object or an array as well)
//These would all work:
var hsvObj = {h: 0.16666666666666666, s: 1, v: 1};
Y.ColorUtil.hsv2rgb(hsvObj);
Y.ColorUtil.hsv2rgb(hsvObj.h, hsvObj.s, hsvObj.v);
Y.ColorUtil.hsv2rgb([0.16666666666666666, 1, 1]);
//The methods are all as follows:
Y.ColorUtil.getRGB()
Y.ColorUtil.hex2rgb()
Y.ColorUtil.hsv2rgb()
Y.ColorUtil.hsl2rgb()
Y.ColorUtil.hsb2rgb()
Y.ColorUtil.rgb2hex()
Y.ColorUtil.rgb2hsl()
Y.ColorUtil.rgb2hsb()
Y.ColorUtil.rgb2hsv()
});| Subject | Author | Date |
|---|---|---|
| Why? | John Lindal | 06/9/10 |
| Re: Why? | Murray Macchio | 06/10/10 |
| Re: Why? | Murray Macchio | 06/10/10 |
| Styling | Eike Hirsch | 06/11/10 |
| Re: Why? | Eduardo Lundgren | 06/11/10 |
| Re: Styling | Eduardo Lundgren | 06/11/10 |
| Re: Why? | Murray Macchio | 06/11/10 |
| Re: Why? | Eduardo Lundgren | 06/11/10 |
| Re: Styling | Eike Hirsch | 06/14/10 |
| Parallel thinking | Peter Peterson | 06/15/10 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info