vanilladocs
vanillapunch.com
API / base/color

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. (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.

Colorstruct traits Clone, Copy, Debug, PartialEq, Default, From<&str>, From<&String>, GeometryKeyValue, ToReflectValue, FromReflectValue<'a>

짝Counterpart: vanilla.js Color소스source

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

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
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);
정적staticpub const fn create(red: f64, green: f64, blue: f64, alpha: f64) -> Color#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

redf64
빨강. (0 ~ 1)Red. (0 ~ 1)
greenf64
초록. (0 ~ 1)Green. (0 ~ 1)
bluef64
파랑. (0 ~ 1)Blue. (0 ~ 1)
alphaf64
알파. (0 ~ 1)Alpha. (0 ~ 1)

반환ReturnsColor 만든 색. (범위를 자르지 않는다)The created color. (the range is not clamped)

짝Counterpart: vanilla.js Color.create

정적staticpub fn create_from_bytes(red: u8, green: u8, blue: u8, alpha: u8) -> Color#0.3.2 부터since 0.3.2소스source

0 ~ 255 정수 성분으로 생성.

Creates from 0 ~ 255 integer components.

redu8
빨강. (0 ~ 255)Red. (0 ~ 255)
greenu8
초록. (0 ~ 255)Green. (0 ~ 255)
blueu8
파랑. (0 ~ 255)Blue. (0 ~ 255)
alphau8
알파. (0 ~ 255)Alpha. (0 ~ 255)

반환ReturnsColor 성분마다 255 로 나눈 색.Color with each component divided by 255.

짝Counterpart: vanilla.js Color.createFromBytes

정적staticpub const fn white() -> Color#0.3.2 부터since 0.3.2소스source

흰색.

White.

반환ReturnsColor (1, 1, 1, 1) 색.The color (1, 1, 1, 1).

짝Counterpart: vanilla.js Color.white

정적staticpub const fn black() -> Color#0.3.2 부터since 0.3.2소스source

검은색.

Black.

반환ReturnsColor (0, 0, 0, 1) 색.The color (0, 0, 0, 1).

짝Counterpart: vanilla.js Color.black

정적staticpub const fn transparent() -> Color#0.3.2 부터since 0.3.2소스source

투명.

Transparent.

반환ReturnsColor (0, 0, 0, 0) 색.The color (0, 0, 0, 0).

짝Counterpart: vanilla.js Color.transparent

정적staticpub fn random() -> Color#0.3.2 부터since 0.3.2소스source

무작위 색. (알파 1)

Random color. (alpha 1)

반환ReturnsColor 빨강, 초록, 파랑이 각각 0 이상 1 미만 난수(math::random)이고 알파가 1 인 색.A color whose red, green and blue are each random numbers from 0 inclusive to 1 exclusive (math::random) and whose alpha is 1.

짝Counterpart: vanilla.js Color.random

정적staticpub fn create_from_hex(hex_string: &str) -> Color#0.3.2 부터since 0.3.2소스source

"#rgb", "#rgba", "#rrggbb", "#rrggbbaa" 문자열로 생성. (원본 createFromHEX, # 으로 시작하지 않으면 흰색)

  • 3, 4 자리는 자리마다 두 번 써 늘인다. 빨강, 초록, 파랑은 두 자리씩 읽고, 알파는 8 자리일 때만 읽는다.

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
# 으로 시작하는 16진수 색 글. (앞뒤 공백과 대소문자는 가리지 않는다)Hex color string starting with #. (leading and trailing whitespace and case are ignored)

반환ReturnsColor 읽은 색. # 으로 시작하지 않으면 흰색, 못 읽은 성분은 NaN.The read color. White if it does not start with #, unreadable components are NaN.

짝Counterpart: vanilla.js Color.createFromHEX

예제Example짧은 꼴, 알파 붙은 꼴, # 이 빠진 글Short form, form with alpha, and a string missing #
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());
정적staticpub fn create_from_rgba(rgba_string: &str) -> Color#0.3.2 부터since 0.3.2소스source

"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 처럼 앞에서 읽히는 데까지.

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)" 나 "rgba(r, g, b, a)" 글. (앞뒤 공백과 대소문자는 가리지 않는다)"rgb(r, g, b)" or "rgba(r, g, b, a)" text. (leading and trailing spaces and case are ignored)

반환ReturnsColor 읽은 색. rgb 로 시작하지 않거나 맞는 꼴이 없으면 흰색, 알파가 없으면 1.The read color. White if it does not start with rgb or nothing matches, alpha 1 if missing.

짝Counterpart: vanilla.js Color.createFromRGBA

예제Examplergba 글과 값이 모자란 글An rgba string and a string missing values
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());
정적staticpub fn lerp(from: Color, to: Color, time: f64) -> Color#0.3.2 부터since 0.3.2소스source

선형 보간.

Linear interpolation.

fromColor
시작 색.Start color.
toColor
끝 색.End color.
timef64
보간 비율. (0 이면 from, 1 이면 to, 0 ~ 1 밖도 자르지 않는다)Interpolation ratio. (0 is from, 1 is to, values outside 0 ~ 1 are not clamped)

반환ReturnsColor 네 성분을 각각 math::lerp 로 보간한 색.Color with each of the four components interpolated with math::lerp.

짝Counterpart: vanilla.js Color.lerp

정적staticpub fn from_cache(color_string: &str) -> Color#0.4.0 부터since 0.4.0소스source

색 문자열로 캐시한 색 반환. (원본 Color.fromCache, "#...", "rgb(...)", 그 밖은 흰색)

  • 같은 문자열이면 다시 풀지 않는다. (풀 때마다 소문자 글을 새로 만든다) 매 프레임 같은 문자열로 색을 정할 때 쓴다. From<&str> 도 이것을 거친다.
  • 원본처럼 1024 개를 넘으면 가장 오래 안 쓴 것부터 버린다. Color 는 Copy 라 나눠 쓰는 인스턴스가 없다.

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
"#..." 나 "rgb(...)" 색 글. (앞뒤 공백과 대소문자는 가리지 않는다, 캐시 키는 넘긴 글 그대로)"#..." or "rgb(...)" color string. (leading and trailing whitespace and case are ignored; the cache key is the passed string as is)

반환ReturnsColor 풀어 둔 색의 복사본. 그 밖의 글이면 흰색.Copy of the parsed color. White for any other string.

짝Counterpart: vanilla.js Color.fromCache

예제Example같은 글은 한 번만 푼다The same string is parsed only once
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());
정적staticpub fn get_cached_color_count() -> usize#0.4.0 부터since 0.4.0소스source

색 문자열 캐시에 든 수. (시험과 통계용)

Number of entries in the color string cache. (for tests and statistics)

반환Returnsusize from_cache 캐시에 든 색 수. (이 스레드의 것, 1024 개까지)Number of colors in the from_cache cache. (for this thread, up to 1024)

짝Counterpart: vanilla.js Color.getCachedColorCount

정적staticpub fn from_hex_cached(color_string: &str, alpha: f64) -> Color#0.4.0 부터since 0.4.0소스source

HEX 문자열과 알파로 캐시한 색 반환. (원본 Color.fromHEXCached, 알파는 0.01 단위로 다듬어 키에 넣는다)

  • from_cache 처럼 1024 개를 넘으면 가장 오래 안 쓴 것부터 버린다.

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
# 으로 시작하는 16진수 색 글. (create_from_hex 로 읽는다)Hex color string starting with #. (read with create_from_hex)
alphaf64
알파. (0 ~ 1, 0.01 단위로 반올림해 쓰고, 글에 든 알파는 버린다)Alpha. (0 ~ 1, rounded to 0.01 steps; any alpha in the string is discarded)

반환ReturnsColor 캐시한 색의 복사본.Copy of the cached color.

짝Counterpart: vanilla.js Color.fromHEXCached

예제Example알파는 0.01 단위로 다듬는다Alpha is rounded to 0.01 steps
use vanilla::Color;

let color = Color::from_hex_cached("#ff0000", 0.504);
assert_eq!(color.red, 1.0);
assert_eq!(color.alpha, 0.5);
필드fieldpub red: f64#0.3.2 부터since 0.3.2소스source

빨강. (0 ~ 1, 범위 밖도 자르지 않는다)

Red. (0 ~ 1, values outside the range are not clamped)

반환Returnsf64

짝Counterpart: vanilla.js Color.red

필드fieldpub green: f64#0.3.2 부터since 0.3.2소스source

초록. (0 ~ 1, 범위 밖도 자르지 않는다)

Green. (0 ~ 1, values outside the range are not clamped)

반환Returnsf64

짝Counterpart: vanilla.js Color.green

필드fieldpub blue: f64#0.3.2 부터since 0.3.2소스source

파랑. (0 ~ 1, 범위 밖도 자르지 않는다)

Blue. (0 ~ 1, values outside the range are not clamped)

반환Returnsf64

짝Counterpart: vanilla.js Color.blue

필드fieldpub alpha: f64#0.3.2 부터since 0.3.2소스source

알파. (0 이 투명, 1 이 불투명, 범위 밖도 자르지 않는다)

Alpha. (0 is transparent, 1 is opaque, values outside the range are not clamped)

반환Returnsf64

짝Counterpart: vanilla.js Color.alpha

메서드methodpub fn to_rgba_string(&self) -> String#0.4.0 부터since 0.4.0소스source

"rgb(r, g, b)" 또는 "rgba(r, g, b, a)" 문자열로 변환. (알파가 1 보다 작을 때만 rgba)

Converts to an "rgb(r, g, b)" or "rgba(r, g, b, a)" string. (rgba only when alpha is less than 1)

반환ReturnsString "rgb(r, g, b)", 알파가 1 보다 작으면 "rgba(r, g, b, a)". (r, g, b 는 성분 x 255 를 반올림해 0 ~ 255 로 자른 정수, a 는 0 ~ 1 로 자른 알파)"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)

짝Counterpart: vanilla.js Color.toRGBAString

메서드methodpub fn to_hex_string(&self) -> String#0.3.2 부터since 0.3.2소스source

"#rrggbb" 또는 "#rrggbbaa" 문자열로 변환. (알파가 1 보다 작을 때만 aa)

Converts to a "#rrggbb" or "#rrggbbaa" string. (aa only when alpha is less than 1)

반환ReturnsString "#rrggbb", 알파가 1 보다 작으면 "#rrggbbaa". (소문자 16진수, 성분마다 0 ~ 255 로 자른 두 자리)"#rrggbb", or "#rrggbbaa" when alpha is less than 1. (lowercase hex, two digits per component clamped to 0 to 255)

짝Counterpart: vanilla.js Color.toHEXString

예제Example알파가 1 이면 붙이지 않는다Alpha is not added when it is 1
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");
메서드methodpub fn with_alpha(&self, alpha: f64) -> Color#0.3.2 부터since 0.3.2소스source

알파만 바꾼 색.

Color with only the alpha changed.

alphaf64
새 알파. (0 ~ 1)New alpha. (0 ~ 1)

반환ReturnsColor 빨강, 초록, 파랑은 그대로이고 알파만 바꾼 새 색.A new color with red, green and blue unchanged and only the alpha changed.

짝Counterpart: vanilla.js Color.withAlpha