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
  • 构造与指定颜色相等的颜色。

    Parameters

    • other: Color

      指定的颜色。

    Returns Color

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

    zh

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

    Parameters

    • hexString: string

      十六进制颜色字符串。

    Returns Color

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

    zh

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

    Parameters

    • Optional r: undefined | number
    • Optional g: undefined | number
    • Optional b: undefined | number
    • Optional a: undefined | number

    Returns Color

Properties

_val

_val: number = 0

Static BLACK

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

Type declaration

Static BLUE

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

Type declaration

Static CYAN

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

Type declaration

Static GRAY

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

Type declaration

Static GREEN

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

Type declaration

Static MAGENTA

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

Type declaration

Static RED

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

Type declaration

Static TRANSPARENT

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

Type declaration

Static WHITE

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

Type declaration

Static YELLOW

YELLOW: object = 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

      相比较的颜色。

    Returns boolean

    两颜色的各通道都相等时返回 true;否则返回 false

fromHEX

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

    Parameters

    • hexString: 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 通道。

    • s: number

      S 通道。

    • v: number

      V 通道。

    Returns this

    this

lerp

  • lerp(to: Color, ratio: number): this
  • 根据指定的插值比率,从当前颜色到目标颜色之间做插值。

    Parameters

    • to: Color

      目标颜色。

    • ratio: number

      插值比率,范围为 [0,1]。

    Returns this

multiply

  • multiply(other: Color): this

set

  • set(other: Color, g?: undefined | number, b?: undefined | number, a?: undefined | number): Color
  • 设置当前颜色使其与指定颜色相等。

    overload

    重载

    Parameters

    • other: Color

      相比较的颜色。

    • Optional g: undefined | number
    • Optional b: undefined | number
    • Optional a: undefined | number

    Returns Color

    当前颜色。

toCSS

  • toCSS(opt: "rgba" | "rgb" | "#rrggbb" | "#rrggbbaa"): string
  • 将当前颜色转换为 CSS 格式。

    Parameters

    • opt: "rgba" | "rgb" | "#rrggbb" | "#rrggbbaa"

      格式选项。

    Returns string

    当前颜色的 CSS 格式。

toHEX

  • toHEX(fmt: "#rrggbb" | "#rrggbbaa"): string
  • 转换当前颜色为十六进制颜色字符串。

    example
    const color = new Color(255, 14, 0, 255);
    color.toHex("rrggbbaa"); // "FF0E00FF"
    color.toHex("rrggbb"); // "FF0E00"

    Parameters

    • fmt: "#rrggbb" | "#rrggbbaa"

      格式选项。

      • '#rrggbbaa' 获取Red、Green、Blue、Alpha通道的十六进制值(两位,高位补 0)并依次连接;
      • '#rrggbb'#rrggbbaa' 类似但不包括 Alpha 通道。

    Returns string

    十六进制颜色字符串。

toHSV

  • toHSV(): object
  • 转换当前颜色为 HSV 颜色。

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

    Returns object

    HSV 颜色。成员 hsv 分别代表 HSV 颜色的 H、S、V 通道。

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

toRGBValue

  • toRGBValue(): number
  • 将当前颜色转换为 RGB 整数值。

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

    Returns number

    RGB 整数值。从最低有效位开始,每8位分别是 Red、Green、Blue 通道的值。

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

      数组起始偏移量

    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

      数组起始偏移量

    Returns Out

Generated using TypeDoc