Skip to main content

颜色

¥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

使用 ColorColorSource

¥Using Color and ColorSource

PixiJS 通过 ColorSource 类型支持多种颜色格式:

¥PixiJS supports many color formats through the ColorSource type:

  • 颜色名称:'red''white''blue' 等。

    ¥Color names: 'red', 'white', 'blue', etc.

  • 十六进制整数:0xffcc00

    ¥Hex integers: 0xffcc00

  • 十六进制字符串:'ffcc00', '#f00', '0xffcc00ff'

    ¥Hex strings: 'ffcc00', '#f00', '0xffcc00ff'

  • RGB(A) 对象:{ r: 255, g: 0, b: 0 }, { r: 255, g: 0, b: 0, a: 0.5 }

    ¥RGB(A) objects: { 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) strings: 'rgb(255,0,0)', 'rgba(255,0,0,0.5)'

  • RGB(A) 数组:[1, 0, 0], [1, 0, 0, 0.5]

    ¥RGB(A) arrays: [1, 0, 0], [1, 0, 0, 0.5]

  • 类型化数组:Uint8Array, Float32Array

    ¥Typed arrays: Uint8Array, Float32Array

  • HSL/HSV 对象和字符串

    ¥HSL/HSV objects and strings

  • Color 实例

    ¥Color instances

每当看到与颜色相关的属性(例如 filltintstroke)时,都可以使用以下任何一种格式。该库将在内部自动将它们转换为适当的格式。

¥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