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

pub opaque type Color

Constants

pub const amber: Color
pub const amber_050: Color
pub const amber_100: Color
pub const amber_150: Color
pub const amber_200: Color
pub const amber_250: Color
pub const amber_300: Color
pub const amber_350: Color
pub const amber_400: Color
pub const amber_450: Color
pub const amber_500: Color
pub const amber_550: Color
pub const amber_600: Color
pub const amber_650: Color
pub const amber_700: Color
pub const amber_750: Color
pub const amber_800: Color
pub const amber_850: Color
pub const amber_900: Color
pub const amber_950: Color
pub const azure: Color
pub const azure_050: Color
pub const azure_100: Color
pub const azure_150: Color
pub const azure_200: Color
pub const azure_250: Color
pub const azure_300: Color
pub const azure_350: Color
pub const azure_400: Color
pub const azure_450: Color
pub const azure_500: Color
pub const azure_550: Color
pub const azure_600: Color
pub const azure_650: Color
pub const azure_700: Color
pub const azure_750: Color
pub const azure_800: Color
pub const azure_850: Color
pub const azure_900: Color
pub const azure_950: Color
pub const black: Color
pub const blue: Color
pub const blue_050: Color
pub const blue_100: Color
pub const blue_150: Color
pub const blue_200: Color
pub const blue_250: Color
pub const blue_300: Color
pub const blue_350: Color
pub const blue_400: Color
pub const blue_450: Color
pub const blue_500: Color
pub const blue_550: Color
pub const blue_600: Color
pub const blue_650: Color
pub const blue_700: Color
pub const blue_750: Color
pub const blue_800: Color
pub const blue_850: Color
pub const blue_900: Color
pub const blue_950: Color
pub const cyan: Color
pub const cyan_050: Color
pub const cyan_100: Color
pub const cyan_150: Color
pub const cyan_200: Color
pub const cyan_250: Color
pub const cyan_300: Color
pub const cyan_350: Color
pub const cyan_400: Color
pub const cyan_450: Color
pub const cyan_500: Color
pub const cyan_550: Color
pub const cyan_600: Color
pub const cyan_650: Color
pub const cyan_700: Color
pub const cyan_750: Color
pub const cyan_800: Color
pub const cyan_850: Color
pub const cyan_900: Color
pub const cyan_950: Color
pub const fuchsia: Color
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 green: Color
pub const green_050: Color
pub const green_100: Color
pub const green_150: Color
pub const green_200: Color
pub const green_250: Color
pub const green_300: Color
pub const green_350: Color
pub const green_400: Color
pub const green_450: Color
pub const green_500: Color
pub const green_550: Color
pub const green_600: Color
pub const green_650: Color
pub const green_700: Color
pub const green_750: Color
pub const green_800: Color
pub const green_850: Color
pub const green_900: Color
pub const green_950: Color
pub const grey: Color
pub const grey_050: Color
pub const grey_100: Color
pub const grey_150: Color
pub const grey_200: Color
pub const grey_250: Color
pub const grey_300: Color
pub const grey_350: Color
pub const grey_400: Color
pub const grey_450: Color
pub const grey_500: Color
pub const grey_550: Color
pub const grey_600: Color
pub const grey_650: Color
pub const grey_700: Color
pub const grey_750: Color
pub const grey_800: Color
pub const grey_850: Color
pub const grey_900: Color
pub const grey_950: Color
pub const indigo: 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 jade: Color
pub const jade_050: Color
pub const jade_100: Color
pub const jade_150: Color
pub const jade_200: Color
pub const jade_250: Color
pub const jade_300: Color
pub const jade_350: Color
pub const jade_400: Color
pub const jade_450: Color
pub const jade_500: Color
pub const jade_550: Color
pub const jade_600: Color
pub const jade_650: Color
pub const jade_700: Color
pub const jade_750: Color
pub const jade_800: Color
pub const jade_850: Color
pub const jade_900: Color
pub const jade_950: Color
pub const lime: Color
pub const lime_050: Color
pub const lime_100: Color
pub const lime_150: Color
pub const lime_200: Color
pub const lime_250: Color
pub const lime_300: Color
pub const lime_350: Color
pub const lime_400: Color
pub const lime_450: Color
pub const lime_500: Color
pub const lime_550: Color
pub const lime_600: Color
pub const lime_650: Color
pub const lime_700: Color
pub const lime_750: Color
pub const lime_800: Color
pub const lime_850: Color
pub const lime_900: Color
pub const lime_950: Color
pub const orange: 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 pink: Color
pub const pink_050: Color
pub const pink_100: Color
pub const pink_150: Color
pub const pink_200: Color
pub const pink_250: Color
pub const pink_300: Color
pub const pink_350: Color
pub const pink_400: Color
pub const pink_450: Color
pub const pink_500: Color
pub const pink_550: Color
pub const pink_600: Color
pub const pink_650: Color
pub const pink_700: Color
pub const pink_750: Color
pub const pink_800: Color
pub const pink_850: Color
pub const pink_900: Color
pub const pink_950: Color
pub const pumpkin: 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: 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 red: Color
pub const red_050: Color
pub const red_100: Color
pub const red_150: Color
pub const red_200: Color
pub const red_250: Color
pub const red_300: Color
pub const red_350: Color
pub const red_400: Color
pub const red_450: Color
pub const red_500: Color
pub const red_550: Color
pub const red_600: Color
pub const red_650: Color
pub const red_700: Color
pub const red_750: Color
pub const red_800: Color
pub const red_850: Color
pub const red_900: Color
pub const red_950: Color
pub const sand: Color
pub const sand_050: Color
pub const sand_100: Color
pub const sand_150: Color
pub const sand_200: Color
pub const sand_250: Color
pub const sand_300: Color
pub const sand_350: Color
pub const sand_400: Color
pub const sand_450: Color
pub const sand_500: Color
pub const sand_550: Color
pub const sand_600: Color
pub const sand_650: Color
pub const sand_700: Color
pub const sand_750: Color
pub const sand_800: Color
pub const sand_850: Color
pub const sand_900: Color
pub const sand_950: Color
pub const slate: Color
pub const slate_050: Color
pub const slate_100: Color
pub const slate_150: Color
pub const slate_200: Color
pub const slate_250: Color
pub const slate_300: Color
pub const slate_350: Color
pub const slate_400: Color
pub const slate_450: Color
pub const slate_500: Color
pub const slate_550: Color
pub const slate_600: Color
pub const slate_650: Color
pub const slate_700: Color
pub const slate_750: Color
pub const slate_800: Color
pub const slate_850: Color
pub const slate_900: Color
pub const slate_950: Color
pub const violet: 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 white: Color
pub const yellow: 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
pub const zinc: Color
pub const zinc_050: Color
pub const zinc_100: Color
pub const zinc_150: Color
pub const zinc_200: Color
pub const zinc_250: Color
pub const zinc_300: Color
pub const zinc_350: Color
pub const zinc_400: Color
pub const zinc_450: Color
pub const zinc_500: Color
pub const zinc_550: Color
pub const zinc_600: Color
pub const zinc_650: Color
pub const zinc_700: Color
pub const zinc_750: Color
pub const zinc_800: Color
pub const zinc_850: Color
pub const zinc_900: Color
pub const zinc_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)
pub fn to_rgba(color: Color) -> #(Int, Int, Int, Float)

Converts a Color to its R, G, B and A values in RGB encoding.

Example:

color.fuchsia_450
|> color.to_rgba
// -> #(237, 42, 172, 1.0)
Search Document