颜色
🌐 Color
PixiJS 中的 Color 类是一个用于表示颜色的灵活工具。它在整个渲染管线中用于诸如色调、填充、描边、渐变等功能。
🌐 The Color class in PixiJS is a flexible utility for representing colors. It is used throughout the rendering pipeline for things like tints, fills, strokes, gradients, and more.
import { Color, Sprite, Texture, Graphics } from 'pixi.js';
const red = new Color('red'); // Named color
const green = new Color(0x00ff00); // Hex
const blue = new Color('#0000ff'); // Hex string
const rgba = new Color({ r: 255, g: 0, b: 0, a: 0.5 }); // RGBA object
console.log(red.toArray()); // [1, 0, 0, 1]
console.log(green.toHex()); // "#00ff00"
const sprite = new Sprite(Texture.WHITE);
sprite.tint = red; // Works directly with a Color instance
使用 Color 和 ColorSource
🌐 Using Color and ColorSource
PixiJS 通过 ColorSource 类型支持多种颜色格式:
🌐 PixiJS supports many color formats through the ColorSource type:
- 颜色名称:
'red'、'white'、'blue',等等。 - 十六进制整数:
0xffcc00 - 十六进制字符串:
'ffcc00'、'#f00'、'0xffcc00ff' - RGB(A) 对象:
{ r: 255, g: 0, b: 0 },{ r: 255, g: 0, b: 0, a: 0.5 } - RGB(A) 字符串:
'rgb(255,0,0)','rgba(255,0,0,0.5)' - RGB(A) 数组:
[1, 0, 0],[1, 0, 0, 0.5] - 类型化数组:
Uint8Array、Float32Array - HSL/HSV 对象和字符串
Color实例
每当你看到与颜色相关的属性(例如,fill、tint、stroke)时,你可以使用任何这些格式。库会在内部自动将它们转换为适当的格式。
🌐 Whenever you see a color-related property (e.g., fill, tint, stroke), you can use any of these formats. The library will automatically convert them to the appropriate format internally.
import { Graphics, Sprite, Texture } from 'pixi.js';
const sprite = new Sprite(Texture.WHITE);
sprite.tint = 'red'; // converted internally
const graphics = new Graphics();
graphics.fill({ color: '#00ff00' }); // Also converted internally
API 参考
🌐 API Reference