# base/color (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/color/

Color.

## Color extends Object

Color.
- Components are usually 0 to 1, but values outside are kept as they are without clamping. (HDR colors above 1 such as glowing colors, same as Unity's Color)
- Only when written as 0 to 255 text (toRGBAString, toHEXString) are they clamped to 0 to 255 and alpha to 0 to 1. (same as converting to Unity's Color32)

Example: Create from strings, blend and output as strings

```js
import { Color } from "./build/vanilla.js";

const red = Color.createFromHEX("#ff0000");
const blue = Color.createFromRGBA("rgba(0, 0, 255, 0.5)");
const purple = Color.lerp(red, blue, 0.5);
console.log(purple.toHEXString(), purple.toRGBAString());
const faded = red.withAlpha(0.25);
console.log(faded.toRGBAString());
```

- `constructor(red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0)` (constructor, since 0.0.8): Constructor.
  - `red` number: Red. (usually 0 to 1, default 1, values outside are kept as they are)
  - `green` number: Green. (usually 0 to 1, default 1, values outside are kept as they are)
  - `blue` number: Blue. (usually 0 to 1, default 1, values outside are kept as they are)
  - `alpha` number: Alpha. (usually 0 to 1, default 1, values outside are kept as they are)
- `get/set red` (property, since 0.0.8): Red getter property.
  Red setter property.
  - returns number: Red. (usually 0 to 1, may exceed 1 for HDR colors)
- `get/set blue` (property, since 0.0.8): Blue getter property.
  Blue setter property.
  - returns number: Blue. (usually 0 to 1, may exceed 1 for HDR colors)
- `get/set green` (property, since 0.0.8): Green getter property.
  Green setter property.
  - returns number: Green. (usually 0 to 1, may exceed 1 for HDR colors)
- `get/set alpha` (property, since 0.0.8): Alpha getter property.
  Alpha setter property.
  - returns number: Alpha. (usually 0 to 1, the stored value as is)
- `toRGBAString()` (method, since 0.0.8): RGBA string getter property.
  - rgba(255, 255, 255, 1.0)
  - returns string: "rgb(r, g, b)", or "rgba(r, g, b, a)" when alpha is less than 1. (r, g, b are integers clamped to 0 to 255, a is clamped to 0 to 1)
- `toHEXString()` (method, since 0.0.8): HEX string getter property.
  - #ffffff
  - #ffffffff
  - returns string: "#rrggbb", or "#rrggbbaa" when alpha is less than 1. (two digits per component clamped to 0 to 255)
- `clone()` (method, since 0.2.1): Clone.
  - returns this: A new color with the same components.
- `static white()` (static, since 0.0.8): Creates white.
  - returns Color: A new (1, 1, 1, 1) color.
- `static black()` (static, since 0.0.8): Creates black.
  - returns Color: A new (0, 0, 0, 1) color.
- `static transparent()` (static, since 0.0.8): Creates transparent.
  - returns Color: A new (0, 0, 0, 0) color.
- `static fromHEXCached(colorString, alpha = 1)` (static, since 0.3.0): Creates a HEX color through the cache. (static)
  - Reduces the GC pressure of creating a new Color from the same string every frame.
  - The returned instance is shared, so never modify it. To modify it, clone() it.
  - Alpha is quantized in 0.01 steps to keep the cache from growing.
  - Like fromCache, discards the least recently used first. (Up to 1024)
  - `colorString` string: HEX color string. (same formats as createFromHEX)
  - `alpha` number: Alpha. (0 ~ 1, default 1, used instead of any alpha in the string)
  - returns Color: A shared color from the cache. (do not modify it)

  Example: Get the same color from the cache on every draw

  ```js
  import { Color } from "./build/vanilla.js";
  
  const first = Color.fromHEXCached("#336699", 0.5);
  const second = Color.fromHEXCached("#336699", 0.5);
  console.log(first === second);
  const editable = first.clone();
  editable.red = 1;
  console.log(first.toHEXString(), editable.toHEXString());
  ```

- `static from(value)` (static, since 0.4.0): Reads a color argument. (static, counterpart of the Rust edition's From<&str> for Color)
  - Setters that take a color use this to accept both a Color and color text (such as "#rrggbb", "#rgb", "rgba(...)").
  - Given a Color, returns that instance as is. (The caller decides whether to copy) Given color text, returns a new copy read with fromCache.
  - `value` Color | string: A color or color text. (The forms fromCache reads; any other text is white)
  - returns Color: The Color itself if a Color, a new Color if color text.

  Example: Passing color text and a Color in the same place

  ```js
  import { Color, Paint } from "./build/vanilla.js";
  
  const fromText = Color.from("#ff8000");
  console.log(fromText.toHEXString());
  const red = new Color(1, 0, 0, 1);
  console.log(Color.from(red) === red);
  const paint = new Paint();
  paint.setColor("#336699");
  console.log(paint.getColor().toHEXString());
  ```

- `static fromCache(colorString)` (static, since 0.4.0): Returns a cached color for a color string. (static, "#rgb", "#rgba", "#rrggbb", "#rrggbbaa", "rgb(...)", "rgba(...)", anything else is white)
  - The same string gives the same instance. When setting a color from the same string every frame, the string is not parsed again.
  - The returned instance is shared, so never modify it. To modify it, clone() it.
  - Discards the least recently used first. (Up to 1024)
  - `colorString` string: Color string.
  - returns Color: A shared color from the cache. (do not modify it)
- `static getCachedColorCount()` (static, since 0.4.0): Number of entries in the color string cache (fromCache). (static, for tests and statistics)
  - returns number: Number of colors in the fromCache cache. (up to 1024)
- `static createFromHEX(colorString)` (static, since 0.0.8): Creates from a HEX string. (static, "#rgb", "#rgba", "#rrggbb", "#rrggbbaa", white if it does not start with "#")
  - `colorString` string: HEX color string. (surrounding whitespace and letter case are ignored)
  - returns Color: A new color. (alpha is 1 for formats without alpha)
- `static create(red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0)` (static, since 0.4.0): Create. (same name as the Rust edition Color::create, same as new Color)
  - `red` number: Red. (0 to 1, default 1)
  - `green` number: Green. (0 to 1, default 1)
  - `blue` number: Blue. (0 to 1, default 1)
  - `alpha` number: Alpha. (0 to 1, default 1, the range is not clamped)
  - returns Color: The new color.

  Example: Making a translucent red

  ```js
  import { Color } from "./build/vanilla.js";
  
  const color = Color.create(1, 0, 0, 0.5);
  console.log(color.red, color.alpha);
  ```

- `static createFromBytes(red, green, blue, alpha)` (static, since 0.4.0): Creates from 0 ~ 255 integer components.
  - `red` number: Red. (0 ~ 255)
  - `green` number: Green. (0 ~ 255)
  - `blue` number: Blue. (0 ~ 255)
  - `alpha` number: Alpha. (0 ~ 255)
  - returns Color: A new color.
- `withAlpha(alpha)` (method, since 0.4.0): Color with only the alpha changed. (Returns a new color and leaves this color as is)
  - `alpha` number: New alpha. (0 ~ 1)
  - returns Color: A new color with the same red, green and blue and only the alpha changed.
- `static lerp(from, to, time)` (static, since 0.4.0): Linear interpolation. (static, per component)
  - `from` Color: Start color.
  - `to` Color: End color.
  - `time` number: 0 gives from, 1 gives to.
  - returns Color: A new color interpolated per component. (components are clamped to 0 ~ 1)
- `static random()` (static, since 0.0.11): Creates a random color. (alpha is 1)
  - returns Color: A new color with random red, green and blue from 0 ~ 1 and alpha 1.
- `static createFromRGBA(colorString)` (static, since 0.0.8): Constructor.
  - rgb(255, 255, 255)
  - rgba(255, 255, 255, 1.0)
  - `colorString` string: "rgb(r, g, b)" or "rgba(r, g, b, a)" string. (r, g, b are integers from 0 ~ 255, a is 0 ~ 1)
  - returns Color: A new color. (white if the format does not match)
