Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Color

通过 Red、Green、Blue 颜色通道表示颜色,并通过 Alpha 通道表示不透明度。
每个通道都为取值范围 [0, 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
  • 判断当前颜色是否与指定颜色相等。

    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
  • 从十六进制颜色字符串中读入当前颜色。
    十六进制颜色字符串应该以可选的 "#" 开头,紧跟最多 8 个代表十六进制数字的字符;
    每两个连续字符代表的数值依次作为 Red、Green、Blue 和 Alpha 通道;
    缺省的颜色通道将视为 0;缺省的透明通道将视为 255。

    Parameters

    • hexString: string

      the hex string

    Returns this

    this

fromHSV

  • fromHSV(h: number, s: number, v: number): this
  • 从 HSV 颜色中读入当前颜色。

    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
  • 根据指定的插值比率,从当前颜色到目标颜色之间做插值。

    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
  • 将当前颜色转换为 CSS 格式。

    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
  • 转换当前颜色为十六进制颜色字符串。

    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 }
  • 转换当前颜色为 HSV 颜色。

    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
  • 将当前颜色转换为 RGB 整数值。

    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

Static equals

  • equals<Out>(a: Out, b: Out, epsilon?: number): boolean
  • 排除浮点数误差的颜色近似等价判断

    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
  • 数组转颜色

    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

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

Static scale

  • scale<Out>(out: Out, a: Out, b: number): Out

Static set

  • set<Out>(out: Out, r: number, g: number, b: number, a: number): Out

Static strictEquals

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

Static subtract

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

Static toArray

  • toArray<Out>(out: Out, a: IColorLike, ofs?: number): Out
  • 颜色转数组

    Type parameters

    • Out: IWritableArrayLike<number>

    Parameters

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

      Array Start Offset

    Returns Out

Generated using TypeDoc