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
WHITEColorSolid white, RGBA is [255, 255, 255, 255].BLACKColorSolid black, RGBA is [0, 0, 0, 255].TRANSPARENTColorTransparent, RGBA is [0, 0, 0, 0].GRAYColorGrey, RGBA is [127.5, 127.5, 127.5].REDColorSolid red, RGBA is [255, 0, 0].GREENColorSolid green, RGBA is [0, 255, 0].BLUEColorSolid blue, RGBA is [0, 0, 255].YELLOWColorYellow, RGBA is [255, 235, 4].ORANGEColorOrange, RGBA is [255, 127, 0].CYANColorCyan, RGBA is [0, 255, 255].MAGENTAColorMagenta, RGBA is [255, 0, 255].rnumberGet or set red channel valuegnumberGet or set green channel valuebnumberGet or set blue channel valueanumberGet or set alpha channel value
Methods
copyCopy content of a color into another.cloneClone a new color.setSet the components of a color to the given values.fromHexConverts the hexadecimal formal color into rgb formal.addAdd components of two colors, respectively.subtractSubtract components of color b from components of color a, respectively.multiplyMultiply components of two colors, respectively.divideDivide components of color a by components of color b, respectively.scaleScales a color by a number.lerpPerforms a linear interpolation between two colors.toArrayTurn an array of colorsfromArrayAn array of colors turnconstructorcloneClone a new color from the current color.equalsTODOlerpTODOtoStringTODOgetRGets red channel valuesetRSets red value and return the current color objectgetGGets green channel valuesetGSets green value and return the current color objectgetBGets blue channel valuesetBSets blue value and return the current color objectgetAGets alpha channel valuesetASets alpha value and return the current color objecttoCSSConvert color to css format.fromHEXRead hex string and store color data into the current color object, the hex string must be formated as rgba or rgb.toHEXconvert Color to HEX color string.toRGBValueConvert to 24bit rgb value.fromHSVRead HSV model color and convert to RGB colortoHSVTransform to HSV model colorsetSet the colormultiplyMultiplies the current color by the specified color
Details
Properties
WHITE
Solid white, RGBA is [255, 255, 255, 255].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:57 |
BLACK
Solid black, RGBA is [0, 0, 0, 255].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:67 |
TRANSPARENT
Transparent, RGBA is [0, 0, 0, 0].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:77 |
GRAY
Grey, RGBA is [127.5, 127.5, 127.5].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:87 |
RED
Solid red, RGBA is [255, 0, 0].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:97 |
GREEN
Solid green, RGBA is [0, 255, 0].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:106 |
BLUE
Solid blue, RGBA is [0, 0, 255].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:115 |
YELLOW
Yellow, RGBA is [255, 235, 4].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:124 |
ORANGE
Orange, RGBA is [255, 127, 0].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:133 |
CYAN
Cyan, RGBA is [0, 255, 255].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:142 |
MAGENTA
Magenta, RGBA is [255, 0, 255].
| meta | description |
|---|---|
| Type | Color |
| Defined in | cocos2d/core/value-types/color.ts:151 |
r
Get or set red channel value
| meta | description |
|---|---|
| Type | number |
| Defined in | cocos2d/core/value-types/color.ts:444 |
g
Get or set green channel value
| meta | description |
|---|---|
| Type | number |
| Defined in | cocos2d/core/value-types/color.ts:456 |
b
Get or set blue channel value
| meta | description |
|---|---|
| Type | number |
| Defined in | cocos2d/core/value-types/color.ts:468 |
a
Get or set alpha channel value
| meta | description |
|---|---|
| Type | number |
| Defined in | cocos2d/core/value-types/color.ts:480 |
Methods
copy
Copy content of a color into another.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:161 |
clone
Clone a new color.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:176 |
set
Set the components of a color to the given values.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:187 |
fromHex
Converts the hexadecimal formal color into rgb formal.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:202 |
add
Add components of two colors, respectively.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:222 |
subtract
Subtract components of color b from components of color a, respectively.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:237 |
multiply
Multiply components of two colors, respectively.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:252 |
divide
Divide components of color a by components of color b, respectively.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:267 |
scale
Scales a color by a number.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:282 |
lerp
Performs a linear interpolation between two colors.
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:297 |
toArray
Turn an array of colors
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:316 |
Parameters
ofsUnknown 数组起始偏移量
fromArray
An array of colors turn
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:334 |
Parameters
ofsUnknown 数组起始偏移量
constructor
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:353 |
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/color.ts:372 |
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/color.ts:387 |
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/color.ts:404 |
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/color.ts:427 |
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/color.ts:492 |
setR
Sets red value and return the current color object
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/value-types/color.ts:501 |
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/color.ts:516 |
setG
Sets green value and return the current color object
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/value-types/color.ts:525 |
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/color.ts:540 |
setB
Sets blue value and return the current color object
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/value-types/color.ts:549 |
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/color.ts:564 |
setA
Sets alpha value and return the current color object
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/value-types/color.ts:573 |
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/color.ts:589 |
Parameters
optString "rgba", "rgb", "#rgb" or "#rrggbb".
Examples
var color = cc.Color.BLACK;
color.toCSS(); // "rgba(0,0,0,1.00)";
color.toCSS("rgba"); // "rgba(0,0,0,1.00)";
color.toCSS("rgb"); // "rgba(0,0,0)";
color.toCSS("#rgb"); // "#000";
color.toCSS("#rrggbb"); // "#000000";
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/color.ts:624 |
Parameters
hexStringString
Examples
var color = cc.Color.BLACK;
color.fromHEX("#FFFF33"); // Color {r: 255, g: 255, b: 51, a: 255};
toHEX
convert Color to HEX color string. e.g. cc.color(255,6,255) to : "#ff06ff"
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/value-types/color.ts:645 |
Parameters
fmtString "#rgb", "#rrggbb" or "#rrggbbaa".
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/color.ts:685 |
Examples
var color = cc.Color.YELLOW;
color.toRGBValue(); // 16771844;
fromHSV
Read HSV model color and convert to RGB color
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/value-types/color.ts:698 |
Parameters
Examples
var color = cc.Color.YELLOW;
color.fromHSV(0, 0, 1); // Color {r: 255, g: 255, b: 255, a: 255};
toHSV
Transform to HSV model color
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/value-types/color.ts:776 |
Examples
var color = cc.Color.YELLOW;
color.toHSV(); // Object {h: 0.1533864541832669, s: 0.9843137254901961, v: 1};
set
Set the color
| meta | description |
|---|---|
| Defined in | cocos2d/core/value-types/color.ts:807 |
Parameters
colorColor
multiply
Multiplies the current color by the specified color
| meta | description |
|---|---|
| Returns | Color |
| Defined in | cocos2d/core/value-types/color.ts:832 |
Parameters
otherColor