Skip to content

Color

A color value used by pen, effects, and the sensing blocks. Construct one with the Color.rgb, Color.hsv, Color.hsl, or Color.hex helpers.

The Color engine class. Most members are available as free functions; this page documents the underlying API for advanced use.

Color.a: number

The alpha (opacity) of this color, 0–1.

Since 1.0.0

Color.b: number

The blue component of this color, 0–255.

Since 1.0.0

Color.g: number

The green component of this color, 0–255.

Since 1.0.0

Color.h: number

The hue of this color, 0–100 (wraps around).

Since 1.0.0

Color.hex(hex: string, a?: number): Color

Create a Color from a hex string (e.g. “#ff0000” or “#f00”).

Since 1.0.0

Example

setPenColor(Color.hex("#3498db"));
Color.hexToHSL(hex: string): { h: number; s: number; l: number }

Convert a hex color string to an HSL object.

Since 1.0.0

Color.hexToRGB(hex: string): { r: number; g: number; b: number }

Convert a hex color string to RGB values (0-255).

Since 1.0.0

Color.hsl(h: number, s: number, l: number, a?: number): Color

Create a Color from HSL values.

Since 1.0.0

Color.hslToHex(h: number, s: number, l: number): string

Convert HSL values to a hex color string.

Since 1.0.0

Color.hslToRGB(h: number, s: number, l: number): { r: number; g: number; b: number }

Convert HSL values to RGB values (0-255).

Since 1.0.0

Color.hsv(h: number, s: number, v: number, a?: number): Color

Create a Color from hue, saturation, and value (HSV) components.

Since 1.0.0

Color.num(n: number | string): Color

Create a Color from a single packed Scratch-style number (0xAARRGGBB).

Since 1.0.0

Color.r: number

The red component of this color, 0–255.

Since 1.0.0

Color.rgb(r: number, g: number, b: number, a?: number): Color

Create a Color from red, green, and blue components (each 0–255).

Since 1.0.0

Example

setPenColor(Color.rgb(255, 0, 0)); // red
Color.rgbToHSL(r: number, g: number, b: number): { h: number; s: number; l: number }

Convert RGB values (0-255) to an HSL object.

Since 1.0.0

Color.rgbToHex(r: number | { r: number; g: number; b: number }, g?: number, b?: number): string

Convert RGB values (0-255) to a hex color string. Accepts either three separate values or a single {r, g, b} object.

Since 1.0.0

Color.s: number

The saturation of this color, 0–100.

Since 1.0.0

Color.toHSL(): { h: number; s: number; l: number }

Convert this color to an HSL object.

Since 1.0.0

Color.toHSLString(forceIncludeAlpha?: boolean): string

Convert this color to an HSL CSS string.

Since 1.0.0

Color.toHexString(forceIncludeAlpha?: boolean): string

Convert this color to a CSS hex string (e.g. “#ff0000”).

Since 1.0.0

Example

console.log(Color.rgb(255, 0, 0).toHexString()); // "#ff0000"
Color.toRGBA(): RGBA

Get this color as an RGBA array with each component from 0 to 255.

Since 1.0.0

Color.toRGBANormalized(): RGBANormalized

Get this color as an RGBA array with each component from 0 to 1.

Since 1.0.0

Color.toRGBString(forceIncludeAlpha?: boolean): string

Convert this color to a CSS rgb()/rgba() string.

Since 1.0.0

Color.toString(): string

Convert this color to a string (its rgb()/rgba() CSS form).

Since 1.0.0

Color.v: number

The value (brightness) of this color, 0–100.

Since 1.0.0