# base/color (vanilla-engine 0.4.0)

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

색상. (각 성분 0 ~ 1, 원본 base/color.js)
- 원본의 red, green, blue, alpha 프로퍼티는 공개 필드로, clone 은 복사(Copy)로 갈음한다.
- 성분은 0 ~ 1 이 보통이지만 원본처럼 밖의 값도 자르지 않고 담는다. (빛나는 색처럼 1 을 넘는 HDR 색, 유니티 Color 와 같다)
- to_rgba_string, to_hex_string 은 원본처럼 알파가 1 보다 작을 때만 알파를 붙이고, 성분은 0 ~ 255 로, rgba 의 알파는 0 ~ 1 로 잘라 적는다.
  (유니티 Color32 로 바꿀 때와 같다, NaN 은 원본처럼 "NaN")
- create_from_rgba 는 원본 createFromRGBA 의 정규식과 같은 것을 읽는다. (소문자로 바꿔 rgb 로 시작해야 하고, 그 안의 첫 rgb(정수, 정수, 정수[, 알파]))
- create_from_hex 는 원본 createFromHEX 처럼 # 으로 시작해야 하고 "#rgb", "#rgba", "#rrggbb", "#rrggbbaa" 를 읽는다.
  두 자리씩 원본 parseInt(글, 16) 처럼 읽어, 그 밖의 길이나 16진수가 아닌 글자도 원본과 같은 값(못 읽은 성분은 NaN)이 된다.
- from_hex_cached, from_cache 는 Color 가 Copy 라 나눠 쓰는 인스턴스 대신 캐시에 든 값을 복사해 준다.
  두 캐시 모두 1024 개를 넘으면 가장 오래 안 쓴 것부터 버린다. (원본 fromCache 의 규칙, 원본 fromHEXCached 에도 같은 규칙을 넣었다)
- 문자열에서 색을 짓는 `From<&str>` 은 원본 Graphic 의 parseColorValue 처럼 from_cache 를 거친다.

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

색상. (빨강, 초록, 파랑, 알파를 0 ~ 1 공개 필드로 둔 Copy 값, 기본값은 흰색)

예제: 문자열로 만들고 섞어 문자열로 내기

```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): 빨강. (0 ~ 1, 범위 밖도 자르지 않는다)
  - returns f64
- `pub green: f64` (field, since 0.3.2): 초록. (0 ~ 1, 범위 밖도 자르지 않는다)
  - returns f64
- `pub blue: f64` (field, since 0.3.2): 파랑. (0 ~ 1, 범위 밖도 자르지 않는다)
  - returns f64
- `pub alpha: f64` (field, since 0.3.2): 알파. (0 이 투명, 1 이 불투명, 범위 밖도 자르지 않는다)
  - returns f64
- `pub const fn create(red: f64, green: f64, blue: f64, alpha: f64) -> Color` (static, since 0.3.2): 생성.
  - `red` f64: 빨강. (0 ~ 1)
  - `green` f64: 초록. (0 ~ 1)
  - `blue` f64: 파랑. (0 ~ 1)
  - `alpha` f64: 알파. (0 ~ 1)
  - returns Color: 만든 색. (범위를 자르지 않는다)
- `pub fn create_from_bytes(red: u8, green: u8, blue: u8, alpha: u8) -> Color` (static, since 0.3.2): 0 ~ 255 정수 성분으로 생성.
  - `red` u8: 빨강. (0 ~ 255)
  - `green` u8: 초록. (0 ~ 255)
  - `blue` u8: 파랑. (0 ~ 255)
  - `alpha` u8: 알파. (0 ~ 255)
  - returns Color: 성분마다 255 로 나눈 색.
- `pub const fn white() -> Color` (static, since 0.3.2): 흰색.
  - returns Color: (1, 1, 1, 1) 색.
- `pub const fn black() -> Color` (static, since 0.3.2): 검은색.
  - returns Color: (0, 0, 0, 1) 색.
- `pub const fn transparent() -> Color` (static, since 0.3.2): 투명.
  - returns Color: (0, 0, 0, 0) 색.
- `pub fn random() -> Color` (static, since 0.3.2): 무작위 색. (알파 1)
  - returns Color: 빨강, 초록, 파랑이 각각 0 이상 1 미만 난수(math::random)이고 알파가 1 인 색.
- `pub fn create_from_hex(hex_string: &str) -> Color` (static, since 0.3.2): "#rgb", "#rgba", "#rrggbb", "#rrggbbaa" 문자열로 생성. (원본 createFromHEX, # 으로 시작하지 않으면 흰색)
  - 3, 4 자리는 자리마다 두 번 써 늘인다. 빨강, 초록, 파랑은 두 자리씩 읽고, 알파는 8 자리일 때만 읽는다.
  - `hex_string` &str: # 으로 시작하는 16진수 색 글. (앞뒤 공백과 대소문자는 가리지 않는다)
  - returns Color: 읽은 색. # 으로 시작하지 않으면 흰색, 못 읽은 성분은 NaN.

  예제: 짧은 꼴, 알파 붙은 꼴, # 이 빠진 글

  ```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): "rgb(r, g, b)" / "rgba(r, g, b, a)" 문자열로 생성. (r, g, b 는 0 ~ 255, a 는 0 ~ 1)
  - 원본 정규식 `rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*([\d.]+))?\s*\)` 의 첫 일치를 읽는다. r, g, b 는 정수, 알파는 원본 parseFloat 처럼 앞에서 읽히는 데까지.
  - `rgba_string` &str: "rgb(r, g, b)" 나 "rgba(r, g, b, a)" 글. (앞뒤 공백과 대소문자는 가리지 않는다)
  - returns Color: 읽은 색. rgb 로 시작하지 않거나 맞는 꼴이 없으면 흰색, 알파가 없으면 1.

  예제: rgba 글과 값이 모자란 글

  ```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): "rgb(r, g, b)" 또는 "rgba(r, g, b, a)" 문자열로 변환. (알파가 1 보다 작을 때만 rgba)
  - returns String: "rgb(r, g, b)", 알파가 1 보다 작으면 "rgba(r, g, b, a)". (r, g, b 는 성분 x 255 를 반올림해 0 ~ 255 로 자른 정수, a 는 0 ~ 1 로 자른 알파)
- `pub fn to_hex_string(&self) -> String` (method, since 0.3.2): "#rrggbb" 또는 "#rrggbbaa" 문자열로 변환. (알파가 1 보다 작을 때만 aa)
  - returns String: "#rrggbb", 알파가 1 보다 작으면 "#rrggbbaa". (소문자 16진수, 성분마다 0 ~ 255 로 자른 두 자리)

  예제: 알파가 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): 알파만 바꾼 색.
  - `alpha` f64: 새 알파. (0 ~ 1)
  - returns Color: 빨강, 초록, 파랑은 그대로이고 알파만 바꾼 새 색.
- `pub fn lerp(from: Color, to: Color, time: f64) -> Color` (static, since 0.3.2): 선형 보간.
  - `from` Color: 시작 색.
  - `to` Color: 끝 색.
  - `time` f64: 보간 비율. (0 이면 from, 1 이면 to, 0 ~ 1 밖도 자르지 않는다)
  - returns Color: 네 성분을 각각 math::lerp 로 보간한 색.
- `pub fn from_cache(color_string: &str) -> Color` (static, since 0.4.0): 색 문자열로 캐시한 색 반환. (원본 Color.fromCache, "#...", "rgb(...)", 그 밖은 흰색)
  - 같은 문자열이면 다시 풀지 않는다. (풀 때마다 소문자 글을 새로 만든다) 매 프레임 같은 문자열로 색을 정할 때 쓴다. `From<&str>` 도 이것을 거친다.
  - 원본처럼 1024 개를 넘으면 가장 오래 안 쓴 것부터 버린다. Color 는 Copy 라 나눠 쓰는 인스턴스가 없다.
  - `color_string` &str: "#..." 나 "rgb(...)" 색 글. (앞뒤 공백과 대소문자는 가리지 않는다, 캐시 키는 넘긴 글 그대로)
  - returns Color: 풀어 둔 색의 복사본. 그 밖의 글이면 흰색.

  예제: 같은 글은 한 번만 푼다

  ```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): 색 문자열 캐시에 든 수. (시험과 통계용)
  - returns usize: from_cache 캐시에 든 색 수. (이 스레드의 것, 1024 개까지)
- `pub fn from_hex_cached(color_string: &str, alpha: f64) -> Color` (static, since 0.4.0): HEX 문자열과 알파로 캐시한 색 반환. (원본 Color.fromHEXCached, 알파는 0.01 단위로 다듬어 키에 넣는다)
  - from_cache 처럼 1024 개를 넘으면 가장 오래 안 쓴 것부터 버린다.
  - `color_string` &str: # 으로 시작하는 16진수 색 글. (create_from_hex 로 읽는다)
  - `alpha` f64: 알파. (0 ~ 1, 0.01 단위로 반올림해 쓰고, 글에 든 알파는 버린다)
  - returns Color: 캐시한 색의 복사본.

  예제: 알파는 0.01 단위로 다듬는다

  ```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);
  ```

