Color Class
Extends ValueType
Representation of RGBA colors.
Each color component is a floating point value with a range from 0 to 255.
You can also use the convenience method cc.color to create a new Color.
Index
Properties
- WHITE- ColorSolid white, RGBA is [255, 255, 255, 255].
- BLACK- ColorSolid black, RGBA is [0, 0, 0, 255].
- TRANSPARENT- ColorTransparent, RGBA is [0, 0, 0, 0].
- GRAY- ColorGrey, RGBA is [127.5, 127.5, 127.5].
- RED- ColorSolid red, RGBA is [255, 0, 0].
- GREEN- ColorSolid green, RGBA is [0, 255, 0].
- BLUE- ColorSolid blue, RGBA is [0, 0, 255].
- YELLOW- ColorYellow, RGBA is [255, 235, 4].
- ORANGE- ColorOrange, RGBA is [255, 127, 0].
- CYAN- ColorCyan, RGBA is [0, 255, 255].
- MAGENTA- ColorMagenta, RGBA is [255, 0, 255].
Methods
- constructor
- cloneClone a new color from the current color.
- equalsTODO
- lerpTODO
- toStringTODO
- getRGets red channel value
- setRSets red value and return the current color object
- getGGets green channel value
- setGSets green value and return the current color object
- getBGets blue channel value
- setBSets blue value and return the current color object
- getAGets alpha channel value
- setASets alpha value and return the current color object
- toCSSConvert color to css format.
- clampClamp this color to make all components between 0 to 255。
- fromHEXRead hex string and store color data into the current color object, the hex string must be formated as rgba or rgb.
- toHEXTODO
- toRGBValueConvert to 24bit rgb value.
- fromHSVTODO
- toHSVTODO
- rgb2hsvTODO
- hsv2rgbTODO
Details
Properties
WHITE
Solid white, RGBA is [255, 255, 255, 255].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:78 | 
BLACK
Solid black, RGBA is [0, 0, 0, 255].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:86 | 
TRANSPARENT
Transparent, RGBA is [0, 0, 0, 0].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:94 | 
GRAY
Grey, RGBA is [127.5, 127.5, 127.5].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:102 | 
RED
Solid red, RGBA is [255, 0, 0].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:110 | 
GREEN
Solid green, RGBA is [0, 255, 0].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:118 | 
BLUE
Solid blue, RGBA is [0, 0, 255].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:126 | 
YELLOW
Yellow, RGBA is [255, 235, 4].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:134 | 
ORANGE
Orange, RGBA is [255, 127, 0].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:142 | 
CYAN
Cyan, RGBA is [0, 255, 255].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:150 | 
MAGENTA
Magenta, RGBA is [255, 0, 255].
| meta | description | 
|---|---|
| Type | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:158 | 
Methods
constructor
| meta | description | 
|---|---|
| Defined in | cocos2d/core/value-types/CCColor.js:53 | 
Parameters
- rNumber red component of the color, default value is 0.
- gNumber green component of the color, defualt value is 0.
- bNumber blue component of the color, default value is 0.
- aNumber alpha component of the color, default value is 255.
clone
Clone a new color from the current color.
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:177 | 
Examples
var color = new cc.Color();
var newColor = color.clone();// Color {r: 0, g: 0, b: 0, a: 255}
equals
TODO
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | cocos2d/core/value-types/CCColor.js:192 | 
Parameters
- otherColor
Examples
var color1 = cc.Color.WHITE;
var color2 = new cc.Color(255, 255, 255);
cc.log(color1.equals(color2)); // true;
color2 = cc.Color.RED;
cc.log(color2.equals(color1)); // false;
lerp
TODO
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:209 | 
Parameters
Examples
// Converts a white color to a black one trough time.
update: function (dt) {
    var color = this.node.color;
    if (color.equals(cc.Color.BLACK)) {
        return;
    }
    this.ratio += dt * 0.1;
    this.node.color = cc.Color.WHITE.lerp(cc.Color.BLACK, ratio);
}
toString
TODO
| meta | description | 
|---|---|
| Returns | String | 
| Defined in | cocos2d/core/value-types/CCColor.js:232 | 
Examples
var color = cc.Color.WHITE;
color.toString(); // "rgba(255, 255, 255, 255)"
getR
Gets red channel value
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | cocos2d/core/value-types/CCColor.js:249 | 
setR
Sets red value and return the current color object
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:258 | 
Parameters
- redNumber the new Red component.
Examples
var color = new cc.Color();
color.setR(255); // Color {r: 255, g: 0, b: 0, a: 255}
getG
Gets green channel value
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | cocos2d/core/value-types/CCColor.js:272 | 
setG
Sets green value and return the current color object
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:281 | 
Parameters
- greenNumber the new Green component.
Examples
var color = new cc.Color();
color.setG(255); // Color {r: 0, g: 255, b: 0, a: 255}
getB
Gets blue channel value
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | cocos2d/core/value-types/CCColor.js:295 | 
setB
Sets blue value and return the current color object
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:304 | 
Parameters
- blueNumber the new Blue component.
Examples
var color = new cc.Color();
color.setB(255); // Color {r: 0, g: 0, b: 255, a: 255}
getA
Gets alpha channel value
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | cocos2d/core/value-types/CCColor.js:318 | 
setA
Sets alpha value and return the current color object
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:327 | 
Parameters
- alphaNumber the new Alpha component.
Examples
var color = new cc.Color();
color.setA(0); // Color {r: 0, g: 0, b: 0, a: 0}
toCSS
Convert color to css format.
| meta | description | 
|---|---|
| Returns | String | 
| Defined in | cocos2d/core/value-types/CCColor.js:347 | 
Parameters
- optString "rgba", "rgb", "#rgb" or "#rrggbb".
Examples
var color = cc.Color.BLACK;
color.toCSS();          // "#000";
color.toCSS("rgba");    // "rgba(0,0,0,1.00)";
color.toCSS("rgb");     // "rgba(0,0,0)";
color.toCSS("#rgb");    // "#000";
color.toCSS("#rrggbb"); // "#000000";
clamp
Clamp this color to make all components between 0 to 255。
| meta | description | 
|---|---|
| Defined in | cocos2d/core/value-types/CCColor.js:376 | 
Examples
var color = new cc.Color(1000, 0, 0, 255);
color.clamp();
cc.log(color); // (255, 0, 0, 255)
fromHEX
Read hex string and store color data into the current color object, the hex string must be formated as rgba or rgb.
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:390 | 
Parameters
- hexStringString
Examples
var color = cc.Color.BLACK;
color.fromHEX("#FFFF33"); // Color {r: 255, g: 255, b: 51, a: 255};
toHEX
TODO
| meta | description | 
|---|---|
| Returns | String | 
| Defined in | cocos2d/core/value-types/CCColor.js:410 | 
Parameters
- fmtString "#rgb" or "#rrggbb".
Examples
var color = cc.Color.BLACK;
color.toHEX("#rgb");     // "000";
color.toHEX("#rrggbb");  // "000000";
toRGBValue
Convert to 24bit rgb value.
| meta | description | 
|---|---|
| Returns | Number | 
| Defined in | cocos2d/core/value-types/CCColor.js:445 | 
Examples
var color = cc.Color.YELLOW;
color.toRGBValue(); // 16771844;
fromHSV
TODO
| meta | description | 
|---|---|
| Returns | Color | 
| Defined in | cocos2d/core/value-types/CCColor.js:458 | 
Parameters
Examples
var color = cc.Color.YELLOW;
color.fromHSV(0, 0, 1); // Color {r: 255, g: 255, b: 255, a: 255};
toHSV
TODO
| meta | description | 
|---|---|
| Returns | Object | 
| Defined in | cocos2d/core/value-types/CCColor.js:477 | 
Examples
var color = cc.Color.YELLOW;
color.toHSV(); // Object {h: 0.1533864541832669, s: 0.9843137254901961, v: 1};
rgb2hsv
TODO
| meta | description | 
|---|---|
| Returns | Object | 
| Defined in | cocos2d/core/value-types/CCColor.js:505 | 
Parameters
Examples
cc.Color.rgb2hsv(255, 255, 255); // Object {h: 0, s: 0, v: 1};
hsv2rgb
TODO
| meta | description | 
|---|---|
| Returns | Object | 
| Defined in | cocos2d/core/value-types/CCColor.js:539 | 
Parameters
Examples
cc.Color.hsv2rgb(0, 0, 1); // Object {r: 255, g: 255, b: 255};
