# base/color (vanilla-engine 0.4.0)

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

Color. (each component 0 ~ 1, the original base/color.js)
- The original's red, green, blue and alpha properties are public fields, and clone is replaced by copying (Copy).
- Components are usually 0 ~ 1, but like the original, values outside are stored without clamping. (HDR colors above 1 such as glowing colors, same as Unity Color)
- to_rgba_string and to_hex_string add alpha only when alpha is less than 1, as in the original, and write components clamped to 0 ~ 255 and the rgba alpha clamped to 0 ~ 1.
  (same as converting to Unity Color32, NaN is "NaN" as in the original)
- create_from_rgba reads the same thing as the regular expression of the original createFromRGBA. (it must start with rgb after lowercasing, and the first rgb(integer, integer, integer[, alpha]) within it is read)
- create_from_hex must start with # like the original createFromHEX, and reads "#rgb", "#rgba", "#rrggbb" and "#rrggbbaa".
  It reads two characters at a time like the original parseInt(string, 16), so other lengths and non-hex characters also give the same values as the original (unreadable components are NaN).
- Since Color is Copy, from_hex_cached and from_cache give a copy of the cached value instead of a shared instance.
  Both caches discard the least recently used entries first when they exceed 1024. (the rule of the original fromCache; the same rule was added to the original fromHEXCached)
- `From<&str>`, which builds a color from a string, goes through from_cache like the original Graphic's parseColorValue.

## Color (struct) (Clone, Copy, Debug, PartialEq, Default, From<&str>, From<&String>, GeometryKeyValue, ToReflectValue, FromReflectValue<'a>)

Color. (a Copy value with red, green, blue and alpha as public 0 ~ 1 fields, defaults to white)

Example: Create from strings, blend and output as strings

```rust
use vanilla::Color;

let red = Color::create_from_hex("#ff0000");
let blue = Color::create_from_rgba("rgba(0, 0, 255, 0.5)");
let purple = Color::lerp(red, blue, 0.5);
assert_eq!(purple.to_hex_string(), "#800080bf");
assert_eq!(purple.to_rgba_string(), "rgba(128, 0, 128, 0.75)");

let faded = red.with_alpha(0.25);
assert_eq!(faded.to_rgba_string(), "rgba(255, 0, 0, 0.25)");
assert_eq!(Color::from("#ff0000"), red);
```

- `pub red: f64` (field, since 0.3.2): Red. (0 ~ 1, values outside the range are not clamped)
  - returns f64
- `pub green: f64` (field, since 0.3.2): Green. (0 ~ 1, values outside the range are not clamped)
  - returns f64
- `pub blue: f64` (field, since 0.3.2): Blue. (0 ~ 1, values outside the range are not clamped)
  - returns f64
- `pub alpha: f64` (field, since 0.3.2): Alpha. (0 is transparent, 1 is opaque, values outside the range are not clamped)
  - returns f64
- `pub const fn create(red: f64, green: f64, blue: f64, alpha: f64) -> Color` (static, since 0.3.2): Constructor.
  - `red` f64: Red. (0 ~ 1)
  - `green` f64: Green. (0 ~ 1)
  - `blue` f64: Blue. (0 ~ 1)
  - `alpha` f64: Alpha. (0 ~ 1)
  - returns Color: The created color. (the range is not clamped)
- `pub fn create_from_bytes(red: u8, green: u8, blue: u8, alpha: u8) -> Color` (static, since 0.3.2): Creates from 0 ~ 255 integer components.
  - `red` u8: Red. (0 ~ 255)
  - `green` u8: Green. (0 ~ 255)
  - `blue` u8: Blue. (0 ~ 255)
  - `alpha` u8: Alpha. (0 ~ 255)
  - returns Color: Color with each component divided by 255.
- `pub const fn white() -> Color` (static, since 0.3.2): White.
  - returns Color: The color (1, 1, 1, 1).
- `pub const fn black() -> Color` (static, since 0.3.2): Black.
  - returns Color: The color (0, 0, 0, 1).
- `pub const fn transparent() -> Color` (static, since 0.3.2): Transparent.
  - returns Color: The color (0, 0, 0, 0).
- `pub fn random() -> Color` (static, since 0.3.2): Random color. (alpha 1)
  - returns Color: A color whose red, green and blue are each random numbers from 0 inclusive to 1 exclusive (math::random) and whose alpha is 1.
- `pub fn create_from_hex(hex_string: &str) -> Color` (static, since 0.3.2): Creates from a "#rgb", "#rgba", "#rrggbb" or "#rrggbbaa" string. (the original createFromHEX, white if it does not start with #)
  - 3 and 4 digit forms are expanded by writing each digit twice. Red, green and blue are read two digits at a time, and alpha only with 8 digits.
  - `hex_string` &str: Hex color string starting with #. (leading and trailing whitespace and case are ignored)
  - returns Color: The read color. White if it does not start with #, unreadable components are NaN.

  Example: Short form, form with alpha, and a string missing #

  ```rust
  use vanilla::Color;
  
  assert_eq!(Color::create_from_hex("#f00"), Color::create(1.0, 0.0, 0.0, 1.0));
  assert_eq!(Color::create_from_hex("#00ff0080"), Color::create(0.0, 1.0, 0.0, 128.0 / 255.0));
  assert_eq!(Color::create_from_hex("ff0000"), Color::white());
  ```

- `pub fn create_from_rgba(rgba_string: &str) -> Color` (static, since 0.3.2): Creates from an "rgb(r, g, b)" / "rgba(r, g, b, a)" string. (r, g, b are 0 ~ 255, a is 0 ~ 1)
  - Reads the first match of the original regular expression `rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*([\d.]+))?\s*\)`. r, g, b are integers, and alpha is read as far as it parses from the front, like the original parseFloat.
  - `rgba_string` &str: "rgb(r, g, b)" or "rgba(r, g, b, a)" text. (leading and trailing spaces and case are ignored)
  - returns Color: The read color. White if it does not start with rgb or nothing matches, alpha 1 if missing.

  Example: An rgba string and a string missing values

  ```rust
  use vanilla::Color;
  
  assert_eq!(Color::create_from_rgba("rgba(255, 0, 0, 0.5)"), Color::create(1.0, 0.0, 0.0, 0.5));
  assert_eq!(Color::create_from_rgba("rgb(0, 255)"), Color::white());
  ```

- `pub fn to_rgba_string(&self) -> String` (method, since 0.4.0): Converts to an "rgb(r, g, b)" or "rgba(r, g, b, a)" string. (rgba only when alpha is less than 1)
  - returns String: "rgb(r, g, b)", or "rgba(r, g, b, a)" when alpha is less than 1. (r, g, b are integers of component x 255 rounded and clamped to 0 to 255, a is the alpha clamped to 0 to 1)
- `pub fn to_hex_string(&self) -> String` (method, since 0.3.2): Converts to a "#rrggbb" or "#rrggbbaa" string. (aa only when alpha is less than 1)
  - returns String: "#rrggbb", or "#rrggbbaa" when alpha is less than 1. (lowercase hex, two digits per component clamped to 0 to 255)

  Example: Alpha is not added when it is 1

  ```rust
  use vanilla::Color;
  
  assert_eq!(Color::create(1.0, 0.5, 0.0, 1.0).to_hex_string(), "#ff8000");
  assert_eq!(Color::create(1.0, 0.5, 0.0, 0.5).to_hex_string(), "#ff800080");
  ```

- `pub fn with_alpha(&self, alpha: f64) -> Color` (method, since 0.3.2): Color with only the alpha changed.
  - `alpha` f64: New alpha. (0 ~ 1)
  - returns Color: A new color with red, green and blue unchanged and only the alpha changed.
- `pub fn lerp(from: Color, to: Color, time: f64) -> Color` (static, since 0.3.2): Linear interpolation.
  - `from` Color: Start color.
  - `to` Color: End color.
  - `time` f64: Interpolation ratio. (0 is from, 1 is to, values outside 0 ~ 1 are not clamped)
  - returns Color: Color with each of the four components interpolated with math::lerp.
- `pub fn from_cache(color_string: &str) -> Color` (static, since 0.4.0): Returns a color cached by color string. (the original Color.fromCache, "#...", "rgb(...)", anything else is white)
  - The same string is not parsed again. (each parse creates a new lowercase string) Use it when setting a color from the same string every frame. `From<&str>` also goes through this.
  - As in the original, when it exceeds 1024 the least recently used entries are discarded first. Since Color is Copy, there are no shared instances.
  - `color_string` &str: "#..." or "rgb(...)" color string. (leading and trailing whitespace and case are ignored; the cache key is the passed string as is)
  - returns Color: Copy of the parsed color. White for any other string.

  Example: The same string is parsed only once

  ```rust
  use vanilla::Color;
  
  let first = Color::from_cache("#336699");
  let second = Color::from_cache("#336699");
  assert_eq!(first, second);
  assert!(Color::get_cached_color_count() >= 1);
  assert_eq!(Color::from_cache("hsl(0, 0%, 0%)"), Color::white());
  ```

- `pub fn get_cached_color_count() -> usize` (static, since 0.4.0): Number of entries in the color string cache. (for tests and statistics)
  - returns usize: Number of colors in the from_cache cache. (for this thread, up to 1024)
- `pub fn from_hex_cached(color_string: &str, alpha: f64) -> Color` (static, since 0.4.0): Returns a color cached by HEX string and alpha. (the original Color.fromHEXCached; alpha is rounded to 0.01 steps for the key)
  - Like from_cache, when it exceeds 1024 the least recently used entries are discarded first.
  - `color_string` &str: Hex color string starting with #. (read with create_from_hex)
  - `alpha` f64: Alpha. (0 ~ 1, rounded to 0.01 steps; any alpha in the string is discarded)
  - returns Color: Copy of the cached color.

  Example: Alpha is rounded to 0.01 steps

  ```rust
  use vanilla::Color;
  
  let color = Color::from_hex_cached("#ff0000", 0.504);
  assert_eq!(color.red, 1.0);
  assert_eq!(color.alpha, 0.5);
  ```

