Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Color

Representation of RGBA colors.
Each color component is an integer value with a range from 0 to 255.

Hierarchy

Index

Constructors

constructor

  • newColor(other: Color): Color
  • newColor(hexString: string): Color
  • newColor(r?: undefined | number, g?: undefined | number, b?: undefined | number, a?: undefined | number): Color
  • en

    Construct a same color from the given color

    zh

    构造与指定颜色相等的颜色。

    Parameters

    • other: Color

      Specified color

    Returns Color

  • en

    Construct a color form the hex color string

    zh

    用十六进制颜色字符串中构造颜色。

    Parameters

    • hexString: string

      Hexadecimal color string.

    Returns Color

  • en

    Construct a color

    zh

    构造具有指定通道的颜色。

    Parameters

    • Optional r: undefined | number

      red component of the color, default value is 0.

    • Optional g: undefined | number

      green component of the color, default value is 0.

    • Optional b: undefined | number

      blue component of the color, default value is 0.

    • Optional a: undefined | number

      alpha component of the color, default value is 255.

    Returns Color

Properties

_val

_val: number = 0

Static BLACK

BLACK: {} = Object.freeze(new Color(0, 0, 0, 255))

Type declaration

Static BLUE

BLUE: {} = Object.freeze(new Color(0, 0, 255, 255))

Type declaration

Static CYAN

CYAN: {} = Object.freeze(new Color(0, 255, 255, 255))

Type declaration

Static GRAY

GRAY: {} = Object.freeze(new Color(127, 127, 127, 255))

Type declaration

Static GREEN

GREEN: {} = Object.freeze(new Color(0, 255, 0, 255))

Type declaration

Static MAGENTA

MAGENTA: {} = Object.freeze(new Color(255, 0, 255, 255))

Type declaration

Static RED

RED: {} = Object.freeze(new Color(255, 0, 0, 255))

Type declaration

Static TRANSPARENT

TRANSPARENT: {} = Object.freeze(new Color(0, 0, 0, 0))

Type declaration

Static WHITE

WHITE: {} = Object.freeze(new Color(255, 255, 255, 255))

Type declaration

Static YELLOW

YELLOW: {} = Object.freeze(new Color(255, 255, 0, 255))

Type declaration

Accessors

a

  • get a(): number
  • set a(alpha: number): void

b

  • get b(): number
  • set b(blue: number): void

g

  • get g(): number
  • set g(green: number): void

r

  • get r(): number
  • set r(red: number): void

w

  • get w(): number
  • set w(value: number): void

x

  • get x(): number
  • set x(value: number): void

y

  • get y(): number
  • set y(value: number): void

z

  • get z(): number
  • set z(value: number): void

Methods

setaunsafe

  • setaunsafe(alpha: any): this

setbunsafe

  • setbunsafe(blue: any): this

setgunsafe

  • setgunsafe(green: any): this

setrunsafe

  • setrunsafe(red: any): this

clone

equals

  • equals(other: Color): boolean
  • Check whether the current color is identical with the given color

    Parameters

    • other: Color

      Specified color

    Returns boolean

    Returns true when all channels of both colours are equal; otherwise returns false.

fromHEX

  • fromHEX(hexString: string): this
  • Read hex string and store color data into the current color object, the hex string must be formatted as rgba or rgb.

    Parameters

    • hexString: string

      the hex string

    Returns this

    this

fromHSV

  • fromHSV(h: number, s: number, v: number): this
  • Read HSV model color and convert to RGB color.

    example
    const color = Color.YELLOW;
    color.fromHSV(0, 0, 1); // Color {r: 255, g: 255, b: 255, a: 255};

    Parameters

    • h: number

      H value。

    • s: number

      S value。

    • v: number

      V value。

    Returns this

    this

lerp

  • lerp(to: Color, ratio: number): this
  • Calculate linear interpolation result between this color and another one with given ratio。

    Parameters

    • to: Color

      Target color

    • ratio: number

      The interpolation coefficient.The range is [0,1].

    Returns this

multiply

  • multiply(other: Color): this

set

  • set(other: Color): Color
  • set(r?: undefined | number, g?: undefined | number, b?: undefined | number, a?: undefined | number): Color

toCSS

  • toCSS(opt?: "rgba" | "rgb" | "#rrggbb" | "#rrggbbaa"): string
  • Convert color to css format.

    example
    let 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";

    Parameters

    • Default value opt: "rgba" | "rgb" | "#rrggbb" | "#rrggbbaa" = "rgba"

      "rgba", "rgb", "#rgb" or "#rrggbb".

    Returns string

    CSS format for the current color.

toHEX

  • toHEX(fmt?: "#rgb" | "#rrggbb" | "#rrggbbaa"): string
  • convert Color to HEX color string.

    example
    const color = new Color(255, 14, 0, 255);
    color.toHEX("#rgb");      // "f00";
    color.toHEX("#rrggbbaa"); // "ff0e00"
    color.toHEX("#rrggbb");   // "ff0e00ff"

    Parameters

    • Default value fmt: "#rgb" | "#rrggbb" | "#rrggbbaa" = "#rrggbb"

      "#rrggbb" or "#rrggbbaa".

      • '#rrggbbaa' obtains the hexadecimal value of the Red, Green, Blue, Alpha channels (two, high complement 0) and connects them sequentially.
      • '#rrggbb' is similar to '#rrggbbaa' but does not include the Alpha channel.

    Returns string

    the Hex color string

toHSV

  • toHSV(): { h: number; s: number; v: number }
  • Transform to HSV model color.

    example
    import { Color } from 'cc';
    const color = Color.YELLOW;
    color.toHSV(); // {h: 0.1533864541832669, s: 0.9843137254901961, v: 1}

    Returns { h: number; s: number; v: number }

    HSV format color

    • h: number
    • s: number
    • v: number

toRGBValue

  • toRGBValue(): number
  • Convert to rgb value.

    example
    const color = Color.YELLOW;
    color.toRGBValue();

    Returns number

    RGB integer value. Starting from the lowest valid bit, each 8 bits is the value of the Red, Green, and Blue channels respectively.

toString

  • toString(): string

Static add

  • add<Out>(out: Out, a: Out, b: Out): Out

Static clone

  • clone<Out>(a: Out): Color

Static copy

  • copy<Out>(out: Out, a: Out): Out

Static divide

  • divide<Out>(out: Out, a: Out, b: Out): Out
  • Divide each components of color a by each components of color b. And save the results to out color.

    Type parameters

    Parameters

    • out: Out
    • a: Out
    • b: Out

    Returns Out

Static equals

  • equals<Out>(a: Out, b: Out, epsilon?: number): boolean
  • Check whether the two given colors are approximately equivalent. Difference of each channel is smaller that the epsilon.

    Type parameters

    Parameters

    • a: Out
    • b: Out
    • Default value epsilon: number = EPSILON

    Returns boolean

Static fromArray

  • fromArray<Out>(arr: IWritableArrayLike<number>, out: Out, ofs?: number): Out
  • Sets the given color with RGBA values in an array, and save the results to out color.

    Type parameters

    Parameters

    • arr: IWritableArrayLike<number>
    • out: Out
    • Default value ofs: number = 0

      Array Start Offset

    Returns Out

Static fromHEX

  • fromHEX<Out>(out: Out, hexString: string): Out
  • Converts the hexadecimal formal color into rgb formal and save the results to out color.

    Type parameters

    Parameters

    • out: Out
    • hexString: string

    Returns Out

Static hex

  • hex<Out>(a: Out): number

Static lerp

  • lerp<Out>(out: Out, from: Out, to: Out, ratio: number): Out

Static multiply

  • multiply<Out>(out: Out, a: Out, b: Out): Out
  • Multiply each components of two colors. And save the results to out color.

    Type parameters

    Parameters

    • out: Out
    • a: Out
    • b: Out

    Returns Out

Static scale

  • scale<Out>(out: Out, a: Out, b: number): Out
  • Multiply all channels in a color with the given scale factor, and save the results to out color.

    Type parameters

    Parameters

    • out: Out
    • a: Out
    • b: number

    Returns Out

Static set

  • set<Out>(out: Out, r: number, g: number, b: number, a: number): Out
  • Set the components of a color to the given values and save the results to out color.

    Type parameters

    Parameters

    • out: Out
    • r: number
    • g: number
    • b: number
    • a: number

    Returns Out

Static strictEquals

  • strictEquals<Out>(a: Out, b: Out): boolean

Static subtract

  • subtract<Out>(out: Out, a: Out, b: Out): Out
  • Subtract each components of color b from each components of color a. And save the results to out color.

    Type parameters

    Parameters

    • out: Out
    • a: Out
    • b: Out

    Returns Out

Static toArray

  • toArray<Out>(out: Out, a: IColorLike, ofs?: number): Out
  • Convert a color object to a RGBA array, and save the results to out color.

    Type parameters

    • Out: IWritableArrayLike<number>

    Parameters

    • out: Out
    • a: IColorLike
    • Default value ofs: number = 0

      Array Start Offset

    Returns Out

Generated using TypeDoc