class Color
class Color
lib/color.tya:2
Color provides the color/Color standard library API.
Source
# Color provides the color/Color standard library API.
class Color
# Color.a stores instance state.
# @type Nil
a: nil
# Color.b stores instance state.
# @type Nil
b: nil
# Color.g stores instance state.
# @type Nil
g: nil
# Color.r stores instance state.
# @type Nil
r: nil
# Color.initialize provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @param a Int a value.
# @return Self the initialized object.
initialize: r = nil, g = nil, b = nil, a = nil ->
if r == nil
r = 0
if g == nil
g = 0
if b == nil
b = 0
if a == nil
a = 0
if r.class != Number or r < 0 or r > 255 or r != r.to_i()
raise error("color.channel: r out of range")
if g.class != Number or g < 0 or g > 255 or g != g.to_i()
raise error("color.channel: g out of range")
if b.class != Number or b < 0 or b > 255 or b != b.to_i()
raise error("color.channel: b out of range")
if a.class != Number or a < 0 or a > 255 or a != a.to_i()
raise error("color.channel: a out of range")
self.r = r.to_i()
self.g = g.to_i()
self.b = b.to_i()
self.a = a.to_i()
# Color.abs provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
abs: value ->
if value < 0
return 0 - value
value
# Color.black provides the color/Color standard library operation.
# @return Any the resulting value.
black: ->
self.rgb(0, 0, 0)
# Color.blend provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @param t Any t value.
# @return Any the resulting value.
blend: a, b, t = nil ->
if t == nil
t = b
b = a
a = self
self.ratio(t, "color.blend")
self.rgba(
self.mix_channel(a.r, b.r, t),
self.mix_channel(a.g, b.g, t),
self.mix_channel(a.b, b.b, t),
self.mix_channel(a.a, b.a, t)
)
# Color.blue provides the color/Color standard library operation.
# @return Any the resulting value.
blue: ->
self.rgb(0, 0, 255)
# Color.channel provides the color/Color standard library operation.
# @param value String value value.
# @param name String name value.
# @return Any the resulting value.
channel: value, name ->
if value.class != Number
raise error("color.channel: " + name + " must be a number")
if value < 0 or value > 255
raise error("color.channel: " + name + " out of range")
if value != value.to_i()
raise error("color.channel: " + name + " must be an integer")
value.to_i()
# Color.contrast_ratio provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
contrast_ratio: a, b = nil ->
if b == nil
b = a
a = self
color_la = self.luminance(a)
color_lb = self.luminance(b)
if color_la < color_lb
color_tmp = color_la
color_la = color_lb
color_lb = color_tmp
(color_la + 0.05) / (color_lb + 0.05)
# Color.css provides the color/Color standard library operation.
# @param text String text value.
# @return String the resulting value.
css: text ->
if text.class != String
raise error("color.css: text must be a string")
color_raw = text.trim().lower()
if color_raw == "black"
return self.black()
if color_raw == "white"
return self.white()
if color_raw == "red"
return self.red()
if color_raw == "green"
return self.green()
if color_raw == "blue"
return self.blue()
if color_raw == "yellow"
return self.yellow()
if color_raw == "cyan"
return self.cyan()
if color_raw == "magenta"
return self.magenta()
if color_raw == "transparent"
return self.transparent()
if color_raw.starts_with("#")
return self.hex(color_raw)
if color_raw.starts_with("rgb(") and color_raw.ends_with(")")
return self.css_rgb(color_raw, 4, false)
if color_raw.starts_with("rgba(") and color_raw.ends_with(")")
return self.css_rgb(color_raw, 5, true)
self.hex(color_raw)
# Color.css_rgb provides the color/Color standard library operation.
# @param text String text value.
# @param start Int start value.
# @param has_alpha Any has alpha value.
# @return Any the resulting value.
css_rgb: text, start, has_alpha ->
color_body = text.slice(start, text.len() - 1)
color_parts = color_body.split(",")
if has_alpha
if color_parts.len() != 4
raise error("color.css: rgba() expects 4 channels")
return self.rgba(color_parts[0].trim().to_i(), color_parts[1].trim().to_i(), color_parts[2].trim().to_i(), color_parts[3].trim().to_i())
if color_parts.len() != 3
raise error("color.css: rgb() expects 3 channels")
self.rgb(
color_parts[0].trim().to_i(),
color_parts[1].trim().to_i(),
color_parts[2].trim().to_i()
)
# Color.cyan provides the color/Color standard library operation.
# @return Any the resulting value.
cyan: ->
self.rgb(0, 255, 255)
# Color.darken provides the color/Color standard library operation.
# @param color Any color value.
# @param amount Any amount value.
# @return Any the resulting value.
darken: color, amount = nil ->
if amount == nil
amount = color
color = self
self.ratio(amount, "color.darken")
self.blend(color, self.black(), amount)
# Color.digit_char provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
digit_char: value ->
if value < 10
return chr(48 + value)
chr(87 + value)
# Color.equal? provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Boolean whether the condition is true.
equal?: a, b = nil ->
if b == nil
b = a
a = self
a.r == b.r and a.g == b.g and a.b == b.b and a.a == b.a
# Color.from_array provides the color/Color standard library operation.
# @param values Array values value.
# @return Array the resulting value.
from_array: values ->
if values.class != Array
raise error("color.from_array: values must be an array")
if values.len() == 3
return self.rgb(values[0], values[1], values[2])
if values.len() == 4
return self.rgba(values[0], values[1], values[2], values[3])
raise error("color.from_array: expected 3 or 4 channels")
# Color.gray provides the color/Color standard library operation.
# @param value String value value.
# @param alpha Any alpha value.
# @return Any the resulting value.
gray: value, alpha ->
if alpha == nil
alpha = 255
Color(value, value, value, alpha)
# Color.gray50 provides the color/Color standard library operation.
# @return Any the resulting value.
gray50: ->
self.rgb(128, 128, 128)
# Color.grayscale provides the color/Color standard library operation.
# @param color Any color value.
# @return Any the resulting value.
grayscale: color = nil ->
if color == nil
color = self
color_value = self.round(
0.299 * color.r + 0.587 * color.g + 0.114 * color.b
)
self.rgba(color_value, color_value, color_value, color.a)
# Color.green provides the color/Color standard library operation.
# @return Any the resulting value.
green: ->
self.rgb(0, 128, 0)
# Color.hex provides the color/Color standard library operation.
# @param text String text value.
# @return String the resulting value.
hex: text ->
if text.class != String
raise error("color.hex: text must be a string")
color_raw = text
if color_raw.starts_with("#")
color_raw = color_raw.slice(1, color_raw.len())
color_len = color_raw.byte_len()
if color_len == 3 or color_len == 4
color_r = self.hex_pair(color_raw[0], color_raw[0])
color_g = self.hex_pair(color_raw[1], color_raw[1])
color_b = self.hex_pair(color_raw[2], color_raw[2])
color_a = 255
if color_len == 4
color_a = self.hex_pair(color_raw[3], color_raw[3])
return self.rgba(color_r, color_g, color_b, color_a)
if color_len == 6 or color_len == 8
color_r = self.hex_pair(color_raw[0], color_raw[1])
color_g = self.hex_pair(color_raw[2], color_raw[3])
color_b = self.hex_pair(color_raw[4], color_raw[5])
color_a = 255
if color_len == 8
color_a = self.hex_pair(color_raw[6], color_raw[7])
return self.rgba(color_r, color_g, color_b, color_a)
raise error("color.hex: invalid hex color")
# Color.hex_byte provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
hex_byte: value ->
color_hi = value / 16.to_i()
color_lo = value % 16
self.digit_char(color_hi) + self.digit_char(color_lo)
# Color.hex_digit provides the color/Color standard library operation.
# @param c Any c value.
# @return Any the resulting value.
hex_digit: c ->
color_code = ord(c)
if color_code >= 48 and color_code <= 57
return color_code - 48
if color_code >= 65 and color_code <= 70
return color_code - 55
if color_code >= 97 and color_code <= 102
return color_code - 87
raise error("color.hex: invalid hex digit")
# Color.hex_pair provides the color/Color standard library operation.
# @param hi Any hi value.
# @param lo Any lo value.
# @return Any the resulting value.
hex_pair: hi, lo ->
self.hex_digit(hi) * 16 + self.hex_digit(lo)
# Color.invert provides the color/Color standard library operation.
# @param color Any color value.
# @return Any the resulting value.
invert: color = nil ->
if color == nil
color = self
self.rgba(255 - color.r, 255 - color.g, 255 - color.b, color.a)
# Color.lighten provides the color/Color standard library operation.
# @param color Any color value.
# @param amount Any amount value.
# @return Any the resulting value.
lighten: color, amount = nil ->
if amount == nil
amount = color
color = self
self.ratio(amount, "color.lighten")
self.blend(color, self.white(), amount)
# Color.linear provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
linear: value ->
color_component = value / 255
if color_component <= 0.03928
return color_component / 12.92
math_pow((color_component + 0.055) / 1.055, 2.4)
# Color.luminance provides the color/Color standard library operation.
# @param color Any color value.
# @return Any the resulting value.
luminance: color = nil ->
if color == nil
color = self
color_r = self.linear(color.r)
color_g = self.linear(color.g)
color_b = self.linear(color.b)
0.2126 * color_r + 0.7152 * color_g + 0.0722 * color_b
# Color.magenta provides the color/Color standard library operation.
# @return Any the resulting value.
magenta: ->
self.rgb(255, 0, 255)
# Color.mix_channel provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @param t Any t value.
# @return Any the resulting value.
mix_channel: a, b, t ->
self.round(a + (b - a) * t)
# Color.nearly_equal? provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @param tolerance Any tolerance value.
# @return Boolean whether the condition is true.
nearly_equal?: a, b, tolerance = nil ->
if tolerance == nil
tolerance = b
b = a
a = self
if tolerance < 0
raise error("color.nearly_equal?: tolerance must be non-negative")
self.abs(a.r - b.r) <= tolerance and self.abs(a.g - b.g) <= tolerance and self.abs(a.b - b.b) <= tolerance and self.abs(a.a - b.a) <= tolerance
# Color.new provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @param a Int a value.
# @return Self the resulting value.
new: r, g, b, a ->
Color(r, g, b, a)
# Color.over provides the color/Color standard library operation.
# @param foreground Any foreground value.
# @param background Any background value.
# @return Any the resulting value.
over: foreground, background = nil ->
if background == nil
background = foreground
foreground = self
color_fa = foreground.a / 255
color_ba = background.a / 255
color_out_a = color_fa + color_ba * (1 - color_fa)
if color_out_a == 0
return self.transparent()
color_r = foreground.r * color_fa
+ background.r * color_ba * (1 - color_fa) / color_out_a
color_g = foreground.g * color_fa
+ background.g * color_ba * (1 - color_fa) / color_out_a
color_b = foreground.b * color_fa
+ background.b * color_ba * (1 - color_fa) / color_out_a
self.rgba(
self.round(color_r),
self.round(color_g),
self.round(color_b),
self.round(color_out_a * 255)
)
# Color.ratio provides the color/Color standard library operation.
# @param value String value value.
# @param name String name value.
# @return Any the resulting value.
ratio: value, name ->
if value.class != Number
raise error(name + ": amount must be a number")
if value < 0 or value > 1
raise error(name + ": amount out of range")
value
# Color.red provides the color/Color standard library operation.
# @return Any the resulting value.
red: ->
self.rgb(255, 0, 0)
# Color.rgb provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @return Any the resulting value.
rgb: r, g, b ->
Color(r, g, b, 255)
# Color.rgba provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @param a Int a value.
# @return Any the resulting value.
rgba: r, g, b, a ->
Color(r, g, b, a)
# Color.round provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
round: value ->
math_round(value).to_i()
# Color.to_array provides the color/Color standard library operation.
# @return Array the resulting value.
to_array: ->
[self.r, self.g, self.b, self.a]
# Color.to_hex provides the color/Color standard library operation.
# @param include_alpha Any include alpha value.
# @return Any the resulting value.
to_hex: include_alpha ->
if include_alpha == nil
include_alpha = false
color_out = "#"
+ self.hex_byte(self.r)
+ self.hex_byte(self.g)
+ self.hex_byte(self.b)
if include_alpha or self.a != 255
color_out = color_out + self.hex_byte(self.a)
color_out
# Color.transparent provides the color/Color standard library operation.
# @return Any the resulting value.
transparent: ->
Color(0, 0, 0, 0)
# Color.white provides the color/Color standard library operation.
# @return Any the resulting value.
white: ->
self.rgb(255, 255, 255)
# Color.with_alpha provides the color/Color standard library operation.
# @param color Any color value.
# @param alpha Any alpha value.
# @return Any the resulting value.
with_alpha: color, alpha = nil ->
if alpha == nil
alpha = color
color = self
self.rgba(color.r, color.g, color.b, alpha)
# Color.yellow provides the color/Color standard library operation.
# @return Any the resulting value.
yellow: ->
self.rgb(255, 255, 0)
Instance Variables
a
Color.a
lib/color.tya:5
Color.a stores instance state.
Source
# Color.a stores instance state.
# @type Nil
a: nil
b
Color.b
lib/color.tya:9
Color.b stores instance state.
Source
# Color.b stores instance state.
# @type Nil
b: nil
g
Color.g
lib/color.tya:13
Color.g stores instance state.
Source
# Color.g stores instance state.
# @type Nil
g: nil
r
Color.r
lib/color.tya:17
Color.r stores instance state.
Source
# Color.r stores instance state.
# @type Nil
r: nil
Methods
abs
Color.abs(value)
lib/color.tya:50
Color.abs provides the color/Color standard library operation.
Source
# Color.abs provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
abs: value ->
if value < 0
return 0 - value
value
black
Color.black()
lib/color.tya:57
Color.black provides the color/Color standard library operation.
Source
# Color.black provides the color/Color standard library operation.
# @return Any the resulting value.
black: ->
self.rgb(0, 0, 0)
blend
Color.blend(a, b, t = nil)
lib/color.tya:65
Color.blend provides the color/Color standard library operation.
Source
# Color.blend provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @param t Any t value.
# @return Any the resulting value.
blend: a, b, t = nil ->
if t == nil
t = b
b = a
a = self
self.ratio(t, "color.blend")
self.rgba(
self.mix_channel(a.r, b.r, t),
self.mix_channel(a.g, b.g, t),
self.mix_channel(a.b, b.b, t),
self.mix_channel(a.a, b.a, t)
)
blue
Color.blue()
lib/color.tya:80
Color.blue provides the color/Color standard library operation.
Source
# Color.blue provides the color/Color standard library operation.
# @return Any the resulting value.
blue: ->
self.rgb(0, 0, 255)
channel
Color.channel(value, name)
lib/color.tya:87
Color.channel provides the color/Color standard library operation.
Source
# Color.channel provides the color/Color standard library operation.
# @param value String value value.
# @param name String name value.
# @return Any the resulting value.
channel: value, name ->
if value.class != Number
raise error("color.channel: " + name + " must be a number")
if value < 0 or value > 255
raise error("color.channel: " + name + " out of range")
if value != value.to_i()
raise error("color.channel: " + name + " must be an integer")
value.to_i()
contrast_ratio
Color.contrast_ratio(a, b = nil)
lib/color.tya:100
Color.contrast_ratio provides the color/Color standard library operation.
Source
# Color.contrast_ratio provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
contrast_ratio: a, b = nil ->
if b == nil
b = a
a = self
color_la = self.luminance(a)
color_lb = self.luminance(b)
if color_la < color_lb
color_tmp = color_la
color_la = color_lb
color_lb = color_tmp
(color_la + 0.05) / (color_lb + 0.05)
css
Color.css(text)
lib/color.tya:115
Color.css provides the color/Color standard library operation.
Source
# Color.css provides the color/Color standard library operation.
# @param text String text value.
# @return String the resulting value.
css: text ->
if text.class != String
raise error("color.css: text must be a string")
color_raw = text.trim().lower()
if color_raw == "black"
return self.black()
if color_raw == "white"
return self.white()
if color_raw == "red"
return self.red()
if color_raw == "green"
return self.green()
if color_raw == "blue"
return self.blue()
if color_raw == "yellow"
return self.yellow()
if color_raw == "cyan"
return self.cyan()
if color_raw == "magenta"
return self.magenta()
if color_raw == "transparent"
return self.transparent()
if color_raw.starts_with("#")
return self.hex(color_raw)
if color_raw.starts_with("rgb(") and color_raw.ends_with(")")
return self.css_rgb(color_raw, 4, false)
if color_raw.starts_with("rgba(") and color_raw.ends_with(")")
return self.css_rgb(color_raw, 5, true)
self.hex(color_raw)
css_rgb
Color.css_rgb(text, start, has_alpha)
lib/color.tya:150
Color.css_rgb provides the color/Color standard library operation.
Source
# Color.css_rgb provides the color/Color standard library operation.
# @param text String text value.
# @param start Int start value.
# @param has_alpha Any has alpha value.
# @return Any the resulting value.
css_rgb: text, start, has_alpha ->
color_body = text.slice(start, text.len() - 1)
color_parts = color_body.split(",")
if has_alpha
if color_parts.len() != 4
raise error("color.css: rgba() expects 4 channels")
return self.rgba(color_parts[0].trim().to_i(), color_parts[1].trim().to_i(), color_parts[2].trim().to_i(), color_parts[3].trim().to_i())
if color_parts.len() != 3
raise error("color.css: rgb() expects 3 channels")
self.rgb(
color_parts[0].trim().to_i(),
color_parts[1].trim().to_i(),
color_parts[2].trim().to_i()
)
cyan
Color.cyan()
lib/color.tya:167
Color.cyan provides the color/Color standard library operation.
Source
# Color.cyan provides the color/Color standard library operation.
# @return Any the resulting value.
cyan: ->
self.rgb(0, 255, 255)
darken
Color.darken(color, amount = nil)
lib/color.tya:174
Color.darken provides the color/Color standard library operation.
Source
# Color.darken provides the color/Color standard library operation.
# @param color Any color value.
# @param amount Any amount value.
# @return Any the resulting value.
darken: color, amount = nil ->
if amount == nil
amount = color
color = self
self.ratio(amount, "color.darken")
self.blend(color, self.black(), amount)
digit_char
Color.digit_char(value)
lib/color.tya:184
Color.digit_char provides the color/Color standard library operation.
Source
# Color.digit_char provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
digit_char: value ->
if value < 10
return chr(48 + value)
chr(87 + value)
equal?
Color.equal?(a, b = nil)
lib/color.tya:193
Color.equal? provides the color/Color standard library operation.
Source
# Color.equal? provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Boolean whether the condition is true.
equal?: a, b = nil ->
if b == nil
b = a
a = self
a.r == b.r and a.g == b.g and a.b == b.b and a.a == b.a
from_array
Color.from_array(values)
lib/color.tya:202
Color.from_array provides the color/Color standard library operation.
Source
# Color.from_array provides the color/Color standard library operation.
# @param values Array values value.
# @return Array the resulting value.
from_array: values ->
if values.class != Array
raise error("color.from_array: values must be an array")
if values.len() == 3
return self.rgb(values[0], values[1], values[2])
if values.len() == 4
return self.rgba(values[0], values[1], values[2], values[3])
raise error("color.from_array: expected 3 or 4 channels")
gray
Color.gray(value, alpha)
lib/color.tya:215
Color.gray provides the color/Color standard library operation.
Source
# Color.gray provides the color/Color standard library operation.
# @param value String value value.
# @param alpha Any alpha value.
# @return Any the resulting value.
gray: value, alpha ->
if alpha == nil
alpha = 255
Color(value, value, value, alpha)
gray50
Color.gray50()
lib/color.tya:222
Color.gray50 provides the color/Color standard library operation.
Source
# Color.gray50 provides the color/Color standard library operation.
# @return Any the resulting value.
gray50: ->
self.rgb(128, 128, 128)
grayscale
Color.grayscale(color = nil)
lib/color.tya:228
Color.grayscale provides the color/Color standard library operation.
Source
# Color.grayscale provides the color/Color standard library operation.
# @param color Any color value.
# @return Any the resulting value.
grayscale: color = nil ->
if color == nil
color = self
color_value = self.round(
0.299 * color.r + 0.587 * color.g + 0.114 * color.b
)
self.rgba(color_value, color_value, color_value, color.a)
green
Color.green()
lib/color.tya:238
Color.green provides the color/Color standard library operation.
Source
# Color.green provides the color/Color standard library operation.
# @return Any the resulting value.
green: ->
self.rgb(0, 128, 0)
hex
Color.hex(text)
lib/color.tya:244
Color.hex provides the color/Color standard library operation.
Source
# Color.hex provides the color/Color standard library operation.
# @param text String text value.
# @return String the resulting value.
hex: text ->
if text.class != String
raise error("color.hex: text must be a string")
color_raw = text
if color_raw.starts_with("#")
color_raw = color_raw.slice(1, color_raw.len())
color_len = color_raw.byte_len()
if color_len == 3 or color_len == 4
color_r = self.hex_pair(color_raw[0], color_raw[0])
color_g = self.hex_pair(color_raw[1], color_raw[1])
color_b = self.hex_pair(color_raw[2], color_raw[2])
color_a = 255
if color_len == 4
color_a = self.hex_pair(color_raw[3], color_raw[3])
return self.rgba(color_r, color_g, color_b, color_a)
if color_len == 6 or color_len == 8
color_r = self.hex_pair(color_raw[0], color_raw[1])
color_g = self.hex_pair(color_raw[2], color_raw[3])
color_b = self.hex_pair(color_raw[4], color_raw[5])
color_a = 255
if color_len == 8
color_a = self.hex_pair(color_raw[6], color_raw[7])
return self.rgba(color_r, color_g, color_b, color_a)
raise error("color.hex: invalid hex color")
hex_byte
Color.hex_byte(value)
lib/color.tya:272
Color.hex_byte provides the color/Color standard library operation.
Source
# Color.hex_byte provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
hex_byte: value ->
color_hi = value / 16.to_i()
color_lo = value % 16
self.digit_char(color_hi) + self.digit_char(color_lo)
hex_digit
Color.hex_digit(c)
lib/color.tya:280
Color.hex_digit provides the color/Color standard library operation.
Source
# Color.hex_digit provides the color/Color standard library operation.
# @param c Any c value.
# @return Any the resulting value.
hex_digit: c ->
color_code = ord(c)
if color_code >= 48 and color_code <= 57
return color_code - 48
if color_code >= 65 and color_code <= 70
return color_code - 55
if color_code >= 97 and color_code <= 102
return color_code - 87
raise error("color.hex: invalid hex digit")
hex_pair
Color.hex_pair(hi, lo)
lib/color.tya:294
Color.hex_pair provides the color/Color standard library operation.
Source
# Color.hex_pair provides the color/Color standard library operation.
# @param hi Any hi value.
# @param lo Any lo value.
# @return Any the resulting value.
hex_pair: hi, lo ->
self.hex_digit(hi) * 16 + self.hex_digit(lo)
initialize
Color.initialize(r = nil, g = nil, b = nil, a = nil)
lib/color.tya:25
Color.initialize provides the color/Color standard library operation.
Source
# Color.initialize provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @param a Int a value.
# @return Self the initialized object.
initialize: r = nil, g = nil, b = nil, a = nil ->
if r == nil
r = 0
if g == nil
g = 0
if b == nil
b = 0
if a == nil
a = 0
if r.class != Number or r < 0 or r > 255 or r != r.to_i()
raise error("color.channel: r out of range")
if g.class != Number or g < 0 or g > 255 or g != g.to_i()
raise error("color.channel: g out of range")
if b.class != Number or b < 0 or b > 255 or b != b.to_i()
raise error("color.channel: b out of range")
if a.class != Number or a < 0 or a > 255 or a != a.to_i()
raise error("color.channel: a out of range")
self.r = r.to_i()
self.g = g.to_i()
self.b = b.to_i()
self.a = a.to_i()
invert
Color.invert(color = nil)
lib/color.tya:300
Color.invert provides the color/Color standard library operation.
Source
# Color.invert provides the color/Color standard library operation.
# @param color Any color value.
# @return Any the resulting value.
invert: color = nil ->
if color == nil
color = self
self.rgba(255 - color.r, 255 - color.g, 255 - color.b, color.a)
lighten
Color.lighten(color, amount = nil)
lib/color.tya:309
Color.lighten provides the color/Color standard library operation.
Source
# Color.lighten provides the color/Color standard library operation.
# @param color Any color value.
# @param amount Any amount value.
# @return Any the resulting value.
lighten: color, amount = nil ->
if amount == nil
amount = color
color = self
self.ratio(amount, "color.lighten")
self.blend(color, self.white(), amount)
linear
Color.linear(value)
lib/color.tya:319
Color.linear provides the color/Color standard library operation.
Source
# Color.linear provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
linear: value ->
color_component = value / 255
if color_component <= 0.03928
return color_component / 12.92
math_pow((color_component + 0.055) / 1.055, 2.4)
luminance
Color.luminance(color = nil)
lib/color.tya:328
Color.luminance provides the color/Color standard library operation.
Source
# Color.luminance provides the color/Color standard library operation.
# @param color Any color value.
# @return Any the resulting value.
luminance: color = nil ->
if color == nil
color = self
color_r = self.linear(color.r)
color_g = self.linear(color.g)
color_b = self.linear(color.b)
0.2126 * color_r + 0.7152 * color_g + 0.0722 * color_b
magenta
Color.magenta()
lib/color.tya:338
Color.magenta provides the color/Color standard library operation.
Source
# Color.magenta provides the color/Color standard library operation.
# @return Any the resulting value.
magenta: ->
self.rgb(255, 0, 255)
mix_channel
Color.mix_channel(a, b, t)
lib/color.tya:346
Color.mix_channel provides the color/Color standard library operation.
Source
# Color.mix_channel provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @param t Any t value.
# @return Any the resulting value.
mix_channel: a, b, t ->
self.round(a + (b - a) * t)
nearly_equal?
Color.nearly_equal?(a, b, tolerance = nil)
lib/color.tya:354
Color.nearly_equal? provides the color/Color standard library operation.
Source
# Color.nearly_equal? provides the color/Color standard library operation.
# @param a Int a value.
# @param b Int b value.
# @param tolerance Any tolerance value.
# @return Boolean whether the condition is true.
nearly_equal?: a, b, tolerance = nil ->
if tolerance == nil
tolerance = b
b = a
a = self
if tolerance < 0
raise error("color.nearly_equal?: tolerance must be non-negative")
self.abs(a.r - b.r) <= tolerance and self.abs(a.g - b.g) <= tolerance and self.abs(a.b - b.b) <= tolerance and self.abs(a.a - b.a) <= tolerance
new
Color.new(r, g, b, a)
lib/color.tya:369
Color.new provides the color/Color standard library operation.
Source
# Color.new provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @param a Int a value.
# @return Self the resulting value.
new: r, g, b, a ->
Color(r, g, b, a)
over
Color.over(foreground, background = nil)
lib/color.tya:376
Color.over provides the color/Color standard library operation.
Source
# Color.over provides the color/Color standard library operation.
# @param foreground Any foreground value.
# @param background Any background value.
# @return Any the resulting value.
over: foreground, background = nil ->
if background == nil
background = foreground
foreground = self
color_fa = foreground.a / 255
color_ba = background.a / 255
color_out_a = color_fa + color_ba * (1 - color_fa)
if color_out_a == 0
return self.transparent()
color_r = foreground.r * color_fa
+ background.r * color_ba * (1 - color_fa) / color_out_a
color_g = foreground.g * color_fa
+ background.g * color_ba * (1 - color_fa) / color_out_a
color_b = foreground.b * color_fa
+ background.b * color_ba * (1 - color_fa) / color_out_a
self.rgba(
self.round(color_r),
self.round(color_g),
self.round(color_b),
self.round(color_out_a * 255)
)
ratio
Color.ratio(value, name)
lib/color.tya:402
Color.ratio provides the color/Color standard library operation.
Source
# Color.ratio provides the color/Color standard library operation.
# @param value String value value.
# @param name String name value.
# @return Any the resulting value.
ratio: value, name ->
if value.class != Number
raise error(name + ": amount must be a number")
if value < 0 or value > 1
raise error(name + ": amount out of range")
value
red
Color.red()
lib/color.tya:411
Color.red provides the color/Color standard library operation.
Source
# Color.red provides the color/Color standard library operation.
# @return Any the resulting value.
red: ->
self.rgb(255, 0, 0)
rgb
Color.rgb(r, g, b)
lib/color.tya:419
Color.rgb provides the color/Color standard library operation.
Source
# Color.rgb provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @return Any the resulting value.
rgb: r, g, b ->
Color(r, g, b, 255)
rgba
Color.rgba(r, g, b, a)
lib/color.tya:428
Color.rgba provides the color/Color standard library operation.
Source
# Color.rgba provides the color/Color standard library operation.
# @param r Int r value.
# @param g Int g value.
# @param b Int b value.
# @param a Int a value.
# @return Any the resulting value.
rgba: r, g, b, a ->
Color(r, g, b, a)
round
Color.round(value)
lib/color.tya:434
Color.round provides the color/Color standard library operation.
Source
# Color.round provides the color/Color standard library operation.
# @param value String value value.
# @return Any the resulting value.
round: value ->
math_round(value).to_i()
to_array
Color.to_array()
lib/color.tya:439
Color.to_array provides the color/Color standard library operation.
Source
# Color.to_array provides the color/Color standard library operation.
# @return Array the resulting value.
to_array: ->
[self.r, self.g, self.b, self.a]
to_hex
Color.to_hex(include_alpha)
lib/color.tya:445
Color.to_hex provides the color/Color standard library operation.
Source
# Color.to_hex provides the color/Color standard library operation.
# @param include_alpha Any include alpha value.
# @return Any the resulting value.
to_hex: include_alpha ->
if include_alpha == nil
include_alpha = false
color_out = "#"
+ self.hex_byte(self.r)
+ self.hex_byte(self.g)
+ self.hex_byte(self.b)
if include_alpha or self.a != 255
color_out = color_out + self.hex_byte(self.a)
color_out
transparent
Color.transparent()
lib/color.tya:458
Color.transparent provides the color/Color standard library operation.
Source
# Color.transparent provides the color/Color standard library operation.
# @return Any the resulting value.
transparent: ->
Color(0, 0, 0, 0)
white
Color.white()
lib/color.tya:463
Color.white provides the color/Color standard library operation.
Source
# Color.white provides the color/Color standard library operation.
# @return Any the resulting value.
white: ->
self.rgb(255, 255, 255)
with_alpha
Color.with_alpha(color, alpha = nil)
lib/color.tya:470
Color.with_alpha provides the color/Color standard library operation.
Source
# Color.with_alpha provides the color/Color standard library operation.
# @param color Any color value.
# @param alpha Any alpha value.
# @return Any the resulting value.
with_alpha: color, alpha = nil ->
if alpha == nil
alpha = color
color = self
self.rgba(color.r, color.g, color.b, alpha)
yellow
Color.yellow()
lib/color.tya:478
Color.yellow provides the color/Color standard library operation.
Source
# Color.yellow provides the color/Color standard library operation.
# @return Any the resulting value.
yellow: ->
self.rgb(255, 255, 0)