# base/color (vanilla.js 0.4.0)

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

색상.

## Color extends Object

색상.
- 성분은 0 ~ 1 이 보통이지만 밖의 값도 자르지 않고 그대로 담는다. (빛나는 색처럼 1 을 넘는 HDR 색, 유니티 Color 와 같다)
- 0 ~ 255 의 글로 낼 때(toRGBAString, toHEXString)만 0 ~ 255 와 알파 0 ~ 1 로 자른다. (유니티 Color32 로 바꿀 때와 같다)

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

```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): 생성.
  - `red` number: 빨강. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)
  - `green` number: 초록. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)
  - `blue` number: 파랑. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)
  - `alpha` number: 알파. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)
- `get/set red` (property, since 0.0.8): 적색 반환 프로퍼티.
  적색 설정 프로퍼티.
  - returns number: 빨강. (보통 0 ~ 1, HDR 색이면 1 을 넘을 수 있다)
- `get/set blue` (property, since 0.0.8): 청색 반환 프로퍼티.
  청색 설정 프로퍼티.
  - returns number: 파랑. (보통 0 ~ 1, HDR 색이면 1 을 넘을 수 있다)
- `get/set green` (property, since 0.0.8): 녹색 반환 프로퍼티.
  녹색 설정 프로퍼티.
  - returns number: 초록. (보통 0 ~ 1, HDR 색이면 1 을 넘을 수 있다)
- `get/set alpha` (property, since 0.0.8): 투명색 반환 프로퍼티.
  투명색 설정 프로퍼티.
  - returns number: 알파. (보통 0 ~ 1, 담은 값 그대로)
- `toRGBAString()` (method, since 0.0.8): RGBA 문자열 반환 프로퍼티.
  - rgba(255, 255, 255, 1.0)
  - returns string: "rgb(r, g, b)", 알파가 1 보다 작으면 "rgba(r, g, b, a)". (r, g, b 는 0 ~ 255 로 자른 정수, a 는 0 ~ 1 로 자른다)
- `toHEXString()` (method, since 0.0.8): HEX 문자열 반환 프로퍼티.
  - #ffffff
  - #ffffffff
  - returns string: "#rrggbb", 알파가 1 보다 작으면 "#rrggbbaa". (성분마다 0 ~ 255 로 자른 두 자리)
- `clone()` (method, since 0.2.1): 복제.
  - returns this: 성분이 같은 새 색.
- `static white()` (static, since 0.0.8): 하얀색 생성.
  - returns Color: (1, 1, 1, 1) 새 색.
- `static black()` (static, since 0.0.8): 검은색 생성.
  - returns Color: (0, 0, 0, 1) 새 색.
- `static transparent()` (static, since 0.0.8): 투명색 생성.
  - returns Color: (0, 0, 0, 0) 새 색.
- `static fromHEXCached(colorString, alpha = 1)` (static, since 0.3.0): 캐시를 거치는 HEX 색 생성. (정적)
  - 매 프레임 같은 문자열로 Color 를 새로 만들며 생기는 GC 압박을 줄인다.
  - 반환된 인스턴스는 공유되므로 절대 고쳐 쓰지 않는다. 고칠 거면 clone() 한다.
  - 알파는 0.01 단위로 양자화해 캐시가 불어나는 것을 막는다.
  - fromCache 처럼 오래 안 쓴 것부터 버린다. (1024 개까지)
  - `colorString` string: HEX 색 문자열. (createFromHEX 와 같은 꼴)
  - `alpha` number: 알파. (0 ~ 1, 기본 1, 문자열에 든 알파 대신 쓴다)
  - returns Color: 캐시에 든 공유 색. (고쳐 쓰지 않는다)

  예제: 그릴 때마다 같은 색을 캐시에서 받기

  ```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): 색 인자 읽기. (정적, 러스트 판 From<&str> for Color 의 짝)
  - 색을 받는 설정 함수들이 이것으로 Color 와 색 글("#rrggbb", "#rgb", "rgba(...)" 같은)을 함께 받는다.
  - Color 를 주면 그 인스턴스를 그대로 돌려준다. (복사할지는 부른 쪽이 정한다) 색 글이면 fromCache 로 읽은 새 복사본을 준다.
  - `value` Color | string: 색이나 색 글. (fromCache 가 읽는 꼴, 그 밖의 글은 흰색)
  - returns Color: Color 면 그것, 색 글이면 새 Color.

  예제: 색 글과 Color 를 같은 자리에 주기

  ```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): 색 문자열로 캐시한 색 반환. (정적, "#rgb", "#rgba", "#rrggbb", "#rrggbbaa", "rgb(...)", "rgba(...)", 그 밖은 흰색)
  - 같은 문자열이면 같은 인스턴스를 준다. 매 프레임 같은 문자열로 색을 정할 때 문자열을 다시 풀지 않는다.
  - 반환된 인스턴스는 공유되므로 절대 고쳐 쓰지 않는다. 고칠 거면 clone() 한다.
  - 오래 안 쓴 것부터 버린다. (1024 개까지)
  - `colorString` string: 색 문자열.
  - returns Color: 캐시에 든 공유 색. (고쳐 쓰지 않는다)
- `static getCachedColorCount()` (static, since 0.4.0): 색 문자열 캐시(fromCache)에 든 수. (정적, 시험과 통계용)
  - returns number: fromCache 캐시에 든 색 수. (1024 개까지)
- `static createFromHEX(colorString)` (static, since 0.0.8): HEX 문자열로 생성. (정적, "#rgb", "#rgba", "#rrggbb", "#rrggbbaa", "#" 로 시작하지 않으면 흰색)
  - `colorString` string: HEX 색 문자열. (앞뒤 공백과 대소문자는 가리지 않는다)
  - returns Color: 새 색. (알파가 없는 꼴이면 알파 1)
- `static create(red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0)` (static, since 0.4.0): 생성. (러스트 Color::create 와 같은 이름, new Color 와 같다)
  - `red` number: 빨강. (0 ~ 1, 기본 1)
  - `green` number: 초록. (0 ~ 1, 기본 1)
  - `blue` number: 파랑. (0 ~ 1, 기본 1)
  - `alpha` number: 알파. (0 ~ 1, 기본 1, 범위를 자르지 않는다)
  - returns Color: 만든 색.

  예제: 반투명 빨강 만들기

  ```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): 0 ~ 255 정수 성분으로 생성.
  - `red` number: 빨강. (0 ~ 255)
  - `green` number: 초록. (0 ~ 255)
  - `blue` number: 파랑. (0 ~ 255)
  - `alpha` number: 알파. (0 ~ 255)
  - returns Color: 새 색.
- `withAlpha(alpha)` (method, since 0.4.0): 알파만 바꾼 색. (새 색을 돌려주고 이 색은 그대로 둔다)
  - `alpha` number: 새 알파. (0 ~ 1)
  - returns Color: 빨강, 초록, 파랑은 같고 알파만 바꾼 새 색.
- `static lerp(from, to, time)` (static, since 0.4.0): 선형 보간. (정적, 성분마다)
  - `from` Color: 시작 색.
  - `to` Color: 끝 색.
  - `time` number: 0 이면 from, 1 이면 to.
  - returns Color: 성분마다 보간한 새 색. (성분은 0 ~ 1 로 자른다)
- `static random()` (static, since 0.0.11): 랜덤 색상 생성. (알파값은 1)
  - returns Color: 빨강, 초록, 파랑이 0 ~ 1 난수이고 알파가 1 인 새 색.
- `static createFromRGBA(colorString)` (static, since 0.0.8): 생성.
  - rgb(255, 255, 255)
  - rgba(255, 255, 255, 1.0)
  - `colorString` string: "rgb(r, g, b)" 나 "rgba(r, g, b, a)" 문자열. (r, g, b 는 0 ~ 255 정수, a 는 0 ~ 1)
  - returns Color: 새 색. (꼴이 맞지 않으면 흰색)
