Skip to main content

Class: Color

一个表示颜色的类,可以使用这个类来初始化颜色

// 默认是白色
const color1 = new Color();

// 16进制颜色
const color2 = new Color('#ff0000');

// RGB
const color3 = new Color('rgb(255, 0, 0)');
const color3 = new Color({ r: 255, g: 255, b: 255 });
const color3 = new Color({ r: 255, g: 255, b: 255, a: 1 });

// css 颜色名称
const color5 = new Color('skyblue');

// HSL
const color6 = new Color('hsl(0, 100%, 50%)');

// HSV
const color6 = new Color({ h: 360, s: 100, v: 100, a: 1 });

// 设置 rgba 值, 支持归一化和非归一化的值
const color7 = new Color(1, 0, 0);
const color7 = new Color(1, 0, 0, 1, true);

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Color(v?, g?, b?, a?, isNormalized?): Color

Parameters

NameTypeDefault valueDescription
vnumber | AnyColor255可以是 rgba 的 r 通道值,也可以是 AnyColor
g?numberundefinedg 通道
b?numberundefinedb 通道
anumber1alpha 通道
isNormalizedbooleanfalse是否是归一化的数值

Returns

Color

Defined in

src/math/Color.ts:67

Properties

r

r: number

Defined in

src/math/Color.ts:55


g

g: number

Defined in

src/math/Color.ts:56


b

b: number

Defined in

src/math/Color.ts:57


a

a: number

Defined in

src/math/Color.ts:58

Methods

fromColor

fromColor(c): Color

解析颜色,凡是可以被 colord 解析的都可以使用

Parameters

NameType
cAnyColor

Returns

Color

Defined in

src/math/Color.ts:98


fromHSL

fromHSL(h, s, l, a?): Color

解析 hsl 颜色

Parameters

NameTypeDefault value
hanyundefined
sanyundefined
lanyundefined
anumber1

Returns

Color

Defined in

src/math/Color.ts:110


fromHSV

fromHSV(h, s, v, a?): Color

解析 hsv 颜色

Parameters

NameTypeDefault value
hanyundefined
sanyundefined
vanyundefined
anumber1

Returns

Color

Defined in

src/math/Color.ts:127


setRGB

setRGB(r, g, b): Color

设置 rgb 数值

Parameters

NameTypeDescription
rnumberr 值,一般为 0-255
gnumberg 值,一般为 0-255
bnumberb 值,一般为 0-255

Returns

Color

Defined in

src/math/Color.ts:143


setRGBA

setRGBA(r, g, b, a, isNormalized?): Color

设置各通道的值

Parameters

NameTypeDescription
ranyred 通道
ganygreen 通道
banyblue 通道
aanyalpha 通道
isNormalized?boolean是否已经归一化(一般从颜色字符串解析的都是未归一化的)

Returns

Color

Defined in

src/math/Color.ts:156


setAlpha

setAlpha(alpha): Color

设置 alpha 通道

Parameters

NameType
alphaany

Returns

Color

Defined in

src/math/Color.ts:168


toHex

toHex(): string

输出 16 进制字符串

Returns

string

Defined in

src/math/Color.ts:180


toHSL

toHSL(): HslaColor

输出 hsl 对象

Returns

HslaColor

Defined in

src/math/Color.ts:187


toHSV

toHSV(): HsvaColor

输出 hsv 对象

Returns

HsvaColor

Defined in

src/math/Color.ts:194


toObject

toObject(isNormalized?): Object

将颜色转换为对象

Parameters

NameTypeDefault valueDescription
isNormalizedbooleanfalse是否进行归一化,默认采用 0-255

Returns

Object

NameType
rnumber
gnumber
bnumber
anumber

Defined in

src/math/Color.ts:202


toArray

toArray(): number[]

转换为数组(一般已经归一化。可以直接传递给 gl)

Returns

number[]

Defined in

src/math/Color.ts:215


toVector

toVector(): Vector4

转换为 Vector4

Returns

Vector4

Defined in

src/math/Color.ts:222


toVector3

toVector3(): Vector3

转换为 Vector3

Returns

Vector3

Defined in

src/math/Color.ts:229


toString

toString(): string

转换为字符串

Returns

string

Defined in

src/math/Color.ts:236