vanilladocs
vanillapunch.com
API / base/color

base/color

색상.

Color.

Colorextends Object

짝Counterpart: vanilla-engine Color

색상.

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

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
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());
생성자constructorconstructor(red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0)#0.0.8 부터since 0.0.8소스source

생성.

Constructor.

rednumber
빨강. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)Red. (usually 0 to 1, default 1, values outside are kept as they are)
greennumber
초록. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)Green. (usually 0 to 1, default 1, values outside are kept as they are)
bluenumber
파랑. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)Blue. (usually 0 to 1, default 1, values outside are kept as they are)
alphanumber
알파. (보통 0 ~ 1, 기본 1, 밖의 값도 그대로 담는다)Alpha. (usually 0 to 1, default 1, values outside are kept as they are)
정적staticstatic white()#0.0.8 부터since 0.0.8소스source

하얀색 생성.

Creates white.

반환ReturnsColor (1, 1, 1, 1) 새 색.A new (1, 1, 1, 1) color.

짝Counterpart: vanilla-engine Color.white

정적staticstatic black()#0.0.8 부터since 0.0.8소스source

검은색 생성.

Creates black.

반환ReturnsColor (0, 0, 0, 1) 새 색.A new (0, 0, 0, 1) color.

짝Counterpart: vanilla-engine Color.black

정적staticstatic transparent()#0.0.8 부터since 0.0.8소스source

투명색 생성.

Creates transparent.

반환ReturnsColor (0, 0, 0, 0) 새 색.A new (0, 0, 0, 0) color.

짝Counterpart: vanilla-engine Color.transparent

정적staticstatic fromHEXCached(colorString, alpha = 1)#0.3.0 부터since 0.3.0소스source

캐시를 거치는 HEX 색 생성. (정적)

  • 매 프레임 같은 문자열로 Color 를 새로 만들며 생기는 GC 압박을 줄인다.
  • 반환된 인스턴스는 공유되므로 절대 고쳐 쓰지 않는다. 고칠 거면 clone() 한다.
  • 알파는 0.01 단위로 양자화해 캐시가 불어나는 것을 막는다.
  • fromCache 처럼 오래 안 쓴 것부터 버린다. (1024 개까지)

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)
colorStringstring
HEX 색 문자열. (createFromHEX 와 같은 꼴)HEX color string. (same formats as createFromHEX)
alphanumber
알파. (0 ~ 1, 기본 1, 문자열에 든 알파 대신 쓴다)Alpha. (0 ~ 1, default 1, used instead of any alpha in the string)

반환ReturnsColor 캐시에 든 공유 색. (고쳐 쓰지 않는다)A shared color from the cache. (do not modify it)

짝Counterpart: vanilla-engine Color.from_hex_cached

예제Example그릴 때마다 같은 색을 캐시에서 받기Get the same color from the cache on every draw
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());
정적staticstatic from(value)#0.4.0 부터since 0.4.0소스source

색 인자 읽기. (정적, 러스트 판 From<&str> for Color 의 짝)

  • 색을 받는 설정 함수들이 이것으로 Color 와 색 글("#rrggbb", "#rgb", "rgba(...)" 같은)을 함께 받는다.
  • Color 를 주면 그 인스턴스를 그대로 돌려준다. (복사할지는 부른 쪽이 정한다) 색 글이면 fromCache 로 읽은 새 복사본을 준다.

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.
valueColor | string
색이나 색 글. (fromCache 가 읽는 꼴, 그 밖의 글은 흰색)A color or color text. (The forms fromCache reads; any other text is white)

반환ReturnsColor Color 면 그것, 색 글이면 새 Color.The Color itself if a Color, a new Color if color text.

예제Example색 글과 Color 를 같은 자리에 주기Passing color text and a Color in the same place
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());
정적staticstatic fromCache(colorString)#0.4.0 부터since 0.4.0소스source

색 문자열로 캐시한 색 반환. (정적, "#rgb", "#rgba", "#rrggbb", "#rrggbbaa", "rgb(...)", "rgba(...)", 그 밖은 흰색)

  • 같은 문자열이면 같은 인스턴스를 준다. 매 프레임 같은 문자열로 색을 정할 때 문자열을 다시 풀지 않는다.
  • 반환된 인스턴스는 공유되므로 절대 고쳐 쓰지 않는다. 고칠 거면 clone() 한다.
  • 오래 안 쓴 것부터 버린다. (1024 개까지)

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)
colorStringstring
색 문자열.Color string.

반환ReturnsColor 캐시에 든 공유 색. (고쳐 쓰지 않는다)A shared color from the cache. (do not modify it)

짝Counterpart: vanilla-engine Color.from_cache

정적staticstatic getCachedColorCount()#0.4.0 부터since 0.4.0소스source

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

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

반환Returnsnumber fromCache 캐시에 든 색 수. (1024 개까지)Number of colors in the fromCache cache. (up to 1024)

짝Counterpart: vanilla-engine Color.get_cached_color_count

정적staticstatic createFromHEX(colorString)#0.0.8 부터since 0.0.8소스source

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

Creates from a HEX string. (static, "#rgb", "#rgba", "#rrggbb", "#rrggbbaa", white if it does not start with "#")

colorStringstring
HEX 색 문자열. (앞뒤 공백과 대소문자는 가리지 않는다)HEX color string. (surrounding whitespace and letter case are ignored)

반환ReturnsColor 새 색. (알파가 없는 꼴이면 알파 1)A new color. (alpha is 1 for formats without alpha)

짝Counterpart: vanilla-engine Color.create_from_hex

정적staticstatic create(red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0)#0.4.0 부터since 0.4.0소스source

생성. (러스트 Color::create 와 같은 이름, new Color 와 같다)

Create. (same name as the Rust edition Color::create, same as new Color)

rednumber
빨강. (0 ~ 1, 기본 1)Red. (0 to 1, default 1)
greennumber
초록. (0 ~ 1, 기본 1)Green. (0 to 1, default 1)
bluenumber
파랑. (0 ~ 1, 기본 1)Blue. (0 to 1, default 1)
alphanumber
알파. (0 ~ 1, 기본 1, 범위를 자르지 않는다)Alpha. (0 to 1, default 1, the range is not clamped)

반환ReturnsColor 만든 색.The new color.

짝Counterpart: vanilla-engine Color.create

예제Example반투명 빨강 만들기Making a translucent red
import { Color } from "./build/vanilla.js";

const color = Color.create(1, 0, 0, 0.5);
console.log(color.red, color.alpha);
정적staticstatic createFromBytes(red, green, blue, alpha)#0.4.0 부터since 0.4.0소스source

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

Creates from 0 ~ 255 integer components.

rednumber
빨강. (0 ~ 255)Red. (0 ~ 255)
greennumber
초록. (0 ~ 255)Green. (0 ~ 255)
bluenumber
파랑. (0 ~ 255)Blue. (0 ~ 255)
alphanumber
알파. (0 ~ 255)Alpha. (0 ~ 255)

반환ReturnsColor 새 색.A new color.

짝Counterpart: vanilla-engine Color.create_from_bytes

정적staticstatic lerp(from, to, time)#0.4.0 부터since 0.4.0소스source

선형 보간. (정적, 성분마다)

Linear interpolation. (static, per component)

fromColor
시작 색.Start color.
toColor
끝 색.End color.
timenumber
0 이면 from, 1 이면 to.0 gives from, 1 gives to.

반환ReturnsColor 성분마다 보간한 새 색. (성분은 0 ~ 1 로 자른다)A new color interpolated per component. (components are clamped to 0 ~ 1)

짝Counterpart: vanilla-engine Color.lerp

정적staticstatic random()#0.0.11 부터since 0.0.11소스source

랜덤 색상 생성. (알파값은 1)

Creates a random color. (alpha is 1)

반환ReturnsColor 빨강, 초록, 파랑이 0 ~ 1 난수이고 알파가 1 인 새 색.A new color with random red, green and blue from 0 ~ 1 and alpha 1.

짝Counterpart: vanilla-engine Color.random

정적staticstatic createFromRGBA(colorString)#0.0.8 부터since 0.0.8소스source

생성.

  • rgb(255, 255, 255)
  • rgba(255, 255, 255, 1.0)

Constructor.

  • rgb(255, 255, 255)
  • rgba(255, 255, 255, 1.0)
colorStringstring
"rgb(r, g, b)" 나 "rgba(r, g, b, a)" 문자열. (r, g, b 는 0 ~ 255 정수, a 는 0 ~ 1)"rgb(r, g, b)" or "rgba(r, g, b, a)" string. (r, g, b are integers from 0 ~ 255, a is 0 ~ 1)

반환ReturnsColor 새 색. (꼴이 맞지 않으면 흰색)A new color. (white if the format does not match)

짝Counterpart: vanilla-engine Color.create_from_rgba

속성propertyget/set red#0.0.8 부터since 0.0.8소스source

적색 반환 프로퍼티. 적색 설정 프로퍼티.

Red getter property. Red setter property.

반환Returnsnumber 빨강. (보통 0 ~ 1, HDR 색이면 1 을 넘을 수 있다)Red. (usually 0 to 1, may exceed 1 for HDR colors)

짝Counterpart: vanilla-engine Color.red

속성propertyget/set blue#0.0.8 부터since 0.0.8소스source

청색 반환 프로퍼티. 청색 설정 프로퍼티.

Blue getter property. Blue setter property.

반환Returnsnumber 파랑. (보통 0 ~ 1, HDR 색이면 1 을 넘을 수 있다)Blue. (usually 0 to 1, may exceed 1 for HDR colors)

짝Counterpart: vanilla-engine Color.blue

속성propertyget/set green#0.0.8 부터since 0.0.8소스source

녹색 반환 프로퍼티. 녹색 설정 프로퍼티.

Green getter property. Green setter property.

반환Returnsnumber 초록. (보통 0 ~ 1, HDR 색이면 1 을 넘을 수 있다)Green. (usually 0 to 1, may exceed 1 for HDR colors)

짝Counterpart: vanilla-engine Color.green

속성propertyget/set alpha#0.0.8 부터since 0.0.8소스source

투명색 반환 프로퍼티. 투명색 설정 프로퍼티.

Alpha getter property. Alpha setter property.

반환Returnsnumber 알파. (보통 0 ~ 1, 담은 값 그대로)Alpha. (usually 0 to 1, the stored value as is)

짝Counterpart: vanilla-engine Color.alpha

메서드methodtoRGBAString()#0.0.8 부터since 0.0.8소스source

RGBA 문자열 반환 프로퍼티.

  • rgba(255, 255, 255, 1.0)

RGBA string getter property.

  • rgba(255, 255, 255, 1.0)

반환Returnsstring "rgb(r, g, b)", 알파가 1 보다 작으면 "rgba(r, g, b, a)". (r, g, b 는 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 clamped to 0 to 255, a is clamped to 0 to 1)

짝Counterpart: vanilla-engine Color.to_rgba_string

메서드methodtoHEXString()#0.0.8 부터since 0.0.8소스source

HEX 문자열 반환 프로퍼티.

  • #ffffff
  • #ffffffff

HEX string getter property.

  • #ffffff
  • #ffffffff

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

짝Counterpart: vanilla-engine Color.to_hex_string

메서드methodclone()#0.2.1 부터since 0.2.1소스source

복제.

Clone.

반환Returnsthis 성분이 같은 새 색.A new color with the same components.

메서드methodwithAlpha(alpha)#0.4.0 부터since 0.4.0소스source

알파만 바꾼 색. (새 색을 돌려주고 이 색은 그대로 둔다)

Color with only the alpha changed. (Returns a new color and leaves this color as is)

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

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

짝Counterpart: vanilla-engine Color.with_alpha