kitten/color
This module contains functions for working with colours.
For most use cases, you should be able to import a handful of the pre-defined colours and use them in your code. These colours are taken directly from Pico CSS. Use their website to browse the colour collection online.
If you need to define custom colours though, this module provides
functions for that too. Since they are fallible, it is highly
recommended that you use them inside your init
function with an
assert
assignment, and store the result in your model (you might
even go as far as to define a custom Palette
type). This way,
you can be sure that if your game starts without errors, the colours
are correct.
Examples
// inside a view() function
|> draw.rect(Vec2(0.0, 0.0), Vec2(50.0, 50.0), color.fuchsia_450)
type Model {
Model(player_pos: Vec2, palette: Palette)
}
type Palette {
Palette(transparent_blue: Color, bright_green: Color)
}
fn init() {
let assert Ok(transparent_blue) = color.blue_600 |> color.set_a(0.5)
let assert Ok(bright_green) = color.from_hex("2EFC0A")
Model(
player_pos: Vec2(0.0, 0.0),
palette: Palette(transparent_blue:, bright_green:)
)
}
Types
Constants
pub const fuchsia_050: Color
pub const fuchsia_100: Color
pub const fuchsia_150: Color
pub const fuchsia_200: Color
pub const fuchsia_250: Color
pub const fuchsia_300: Color
pub const fuchsia_350: Color
pub const fuchsia_400: Color
pub const fuchsia_450: Color
pub const fuchsia_500: Color
pub const fuchsia_550: Color
pub const fuchsia_600: Color
pub const fuchsia_650: Color
pub const fuchsia_700: Color
pub const fuchsia_750: Color
pub const fuchsia_800: Color
pub const fuchsia_850: Color
pub const fuchsia_900: Color
pub const fuchsia_950: Color
pub const indigo_050: Color
pub const indigo_100: Color
pub const indigo_150: Color
pub const indigo_200: Color
pub const indigo_250: Color
pub const indigo_300: Color
pub const indigo_350: Color
pub const indigo_400: Color
pub const indigo_450: Color
pub const indigo_500: Color
pub const indigo_550: Color
pub const indigo_600: Color
pub const indigo_650: Color
pub const indigo_700: Color
pub const indigo_750: Color
pub const indigo_800: Color
pub const indigo_850: Color
pub const indigo_900: Color
pub const indigo_950: Color
pub const orange_050: Color
pub const orange_100: Color
pub const orange_150: Color
pub const orange_200: Color
pub const orange_250: Color
pub const orange_300: Color
pub const orange_350: Color
pub const orange_400: Color
pub const orange_450: Color
pub const orange_500: Color
pub const orange_550: Color
pub const orange_600: Color
pub const orange_650: Color
pub const orange_700: Color
pub const orange_750: Color
pub const orange_800: Color
pub const orange_850: Color
pub const orange_900: Color
pub const orange_950: Color
pub const pumpkin_050: Color
pub const pumpkin_100: Color
pub const pumpkin_150: Color
pub const pumpkin_200: Color
pub const pumpkin_250: Color
pub const pumpkin_300: Color
pub const pumpkin_350: Color
pub const pumpkin_400: Color
pub const pumpkin_450: Color
pub const pumpkin_500: Color
pub const pumpkin_550: Color
pub const pumpkin_600: Color
pub const pumpkin_650: Color
pub const pumpkin_700: Color
pub const pumpkin_750: Color
pub const pumpkin_800: Color
pub const pumpkin_850: Color
pub const pumpkin_900: Color
pub const pumpkin_950: Color
pub const purple_050: Color
pub const purple_100: Color
pub const purple_150: Color
pub const purple_200: Color
pub const purple_250: Color
pub const purple_300: Color
pub const purple_350: Color
pub const purple_400: Color
pub const purple_450: Color
pub const purple_500: Color
pub const purple_550: Color
pub const purple_600: Color
pub const purple_650: Color
pub const purple_700: Color
pub const purple_750: Color
pub const purple_800: Color
pub const purple_850: Color
pub const purple_900: Color
pub const purple_950: Color
pub const violet_050: Color
pub const violet_100: Color
pub const violet_150: Color
pub const violet_200: Color
pub const violet_250: Color
pub const violet_300: Color
pub const violet_350: Color
pub const violet_400: Color
pub const violet_450: Color
pub const violet_500: Color
pub const violet_550: Color
pub const violet_600: Color
pub const violet_650: Color
pub const violet_700: Color
pub const violet_750: Color
pub const violet_800: Color
pub const violet_850: Color
pub const violet_900: Color
pub const violet_950: Color
pub const yellow_050: Color
pub const yellow_100: Color
pub const yellow_150: Color
pub const yellow_200: Color
pub const yellow_250: Color
pub const yellow_300: Color
pub const yellow_350: Color
pub const yellow_400: Color
pub const yellow_450: Color
pub const yellow_500: Color
pub const yellow_550: Color
pub const yellow_600: Color
pub const yellow_650: Color
pub const yellow_700: Color
pub const yellow_750: Color
pub const yellow_800: Color
pub const yellow_850: Color
pub const yellow_900: Color
pub const yellow_950: Color
Functions
pub fn from_hex(hex_string: String) -> Result(Color, Nil)
Creates a Colour
from a hex string of the form “#RRGGBB”
with R, G and B values in RGB encoding and A automatically set to 1.0.
The values must be between 0 and 255, otherwise the function fails.
Examples:
color.from_hex("#ED2AAC"))
// -> Ok(Color(237, 42, 172, 1.0))
color.from_hex("#ZZZZZZ")
// -> Error(Nil)
pub fn from_hexa(hex_string: String) -> Result(Color, Nil)
Creates a Colour
from a hex string of the form “#RRGGBBAA”
with R, G, and B values in RGB encoding. R, G, B and A must be
between 0 and 255 otherwise the function fails.
Examples:
color.from_hexa("#ED2AACFF"))
// -> Ok(Color(237, 42, 172, 1.0))
color.from_hexa("#ZZZZZZZZ")
// -> Error(Nil)
pub fn from_hsl(h: Int, s: Int, l: Int) -> Result(Color, Nil)
Creates a Colour
from H, S and L values in HSL encoding, with A
automatically set to 1.0. H must be between 0 and 359, and S and L
between 0 and 100, otherwise the function fails.
Examples:
color.from_hsl(240, 90, 80)
// -> Ok(Color(158, 158, 250, 1.0))
color.from_hsl(400, 90, 80)
// -> Error(Nil)
pub fn from_hsla(
h: Int,
s: Int,
l: Int,
a: Float,
) -> Result(Color, Nil)
Creates a Colour
from H, S, L and A values in HSL encoding.
H must be between 0 and 359, S and L between 0 and 100, and A
between 0.0 and 1.0, otherwise the function fails.
Examples:
color.from_hsla(240, 90, 80, 0.8)
// -> Ok(Color(158, 158, 250, 0.8))
color.from_hsla(240, 90, 80, 2.0)
// -> Error(Nil)
pub fn from_rgb(r: Int, g: Int, b: Int) -> Result(Color, Nil)
Creates a Colour
from R, G and B values in RGB encoding, with A
automatically set to 1.0. The values must be between 0 and 255,
otherwise the function fails.
Examples:
color.from_rgb(237, 42, 172)
// -> Ok(Color(237, 42, 172, 1.0))
color.from_rgb(237, 42, 256)
// -> Error(Nil)
pub fn from_rgba(
r: Int,
g: Int,
b: Int,
a: Float,
) -> Result(Color, Nil)
Creates a Colour
from R, G, B and A values in RGB encoding.
R, G and B must be between 0 and 255 and A between 0.0 and 1.0,
otherwise the function fails.
Examples:
color.from_rgba(237, 42, 172, 0.8)
// -> Ok(Color(237, 42, 172, 0.8))
color.from_rgba(237, 42, 172, 2.0)
// -> Error(Nil)
pub fn get_a(color: Color) -> Float
Returns the transparency (A) value of the colour.
Example:
color.fuchsia_450
|> color.get_a
// -> 1.0
pub fn get_b(color: Color) -> Int
Returns the B value of the colour in RGB encoding.
Example:
color.fuchsia_450
|> color.get_b
// -> 172
pub fn get_g(color: Color) -> Int
Returns the G value of the colour in RGB encoding.
Example:
color.fuchsia_450
|> color.get_g
// -> 42
pub fn get_h(color: Color) -> Int
Returns the H value of the colour in HSL encoding.
Example:
color.fuchsia_450
|> color.get_h
// -> 320
pub fn get_l(color: Color) -> Int
Returns the L value of the colour in HSL encoding.
Example:
color.fuchsia_450
|> color.get_l
// -> 55
pub fn get_r(color: Color) -> Int
Returns the R value of the colour in RGB encoding.
Example:
color.fuchsia_450
|> color.get_r
// -> 237
pub fn get_s(color: Color) -> Int
Returns the S value of the colour in HSL encoding.
Example:
color.fuchsia_450
|> color.get_s
// -> 84
pub fn random() -> Color
Returns a random color. The R, G and B values are picked randomly, but A is always set to 1.0.
Example:
color.random()
// -> Color(237, 42, 172, 1.0)
pub fn set_a(color: Color, new_a: Float) -> Result(Color, Nil)
Sets the transparency (A) value of the colour. A must be between 0.0 and 1.0, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_a(0.8)
// -> Color(237, 42, 172, 0.8)
pub fn set_b(color: Color, new_b: Int) -> Result(Color, Nil)
Sets the B value of the colour in RGB encoding. G must be between 0 and 255, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_g(255)
// -> Color(237, 42, 255, 1.0)
pub fn set_g(color: Color, new_g: Int) -> Result(Color, Nil)
Sets the G value of the colour in RGB encoding. G must be between 0 and 255, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_g(255)
// -> Color(237, 255, 172, 1.0)
pub fn set_h(color: Color, new_h: Int) -> Result(Color, Nil)
Sets the H value of the colour in HSL encoding. H must be between 0 and 359, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_h(100)
// -> Color(108, 237, 44, 1.0)
pub fn set_l(color: Color, new_l: Int) -> Result(Color, Nil)
Sets the L value of the colour in HSL encoding. L must be between 0 and 100, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_l(50)
// -> Color(235, 20, 163, 1.0)
pub fn set_r(color: Color, new_r: Int) -> Result(Color, Nil)
Sets the R value of the colour in RGB encoding. R must be between 0 and 255, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_r(255)
// -> Color(255, 42, 172, 1.0)
pub fn set_s(color: Color, new_s: Int) -> Result(Color, Nil)
Sets the S value of the colour in HSL encoding. S must be between 0 and 100, otherwise the function fails.
Example:
color.fuchsia_450
|> color.set_s(50)
// -> Color(198, 83, 159, 1.0)
pub fn to_hex(color: Color) -> String
Converts a Color
to a hex string of the form “#RRGGBB” in RGB encoding.
Example:
color.fuchsia_450
|> color.to_hex
// -> "#ED2AAC"
pub fn to_hexa(color: Color) -> String
Converts a Color
to a hex string of the form “#RRGGBBAA” in RGB encoding.
Example:
color.fuchsia_450
|> color.to_hexa
// -> "#ED2AACFF"
pub fn to_hsl(color: Color) -> #(Int, Int, Int)
Converts a Color
to its H, S and L values in HSL encoding.
Example:
color.fuchsia_450
|> color.to_hsl
// -> #(320, 84, 55)
pub fn to_hsla(color: Color) -> #(Int, Int, Int, Float)
Converts a Color
to its H, S, L and A values in HSL encoding.
Example:
color.fuchsia_450
|> color.to_hsla
// -> #(320, 84, 55, 1.0)
pub fn to_rgb(color: Color) -> #(Int, Int, Int)
Converts a Color
to its R, G and B values in RGB encoding.
Example:
color.fuchsia_450
|> color.to_rgb
// -> #(237, 42, 172)