gleam_community/colour


This package was heavily inspired by the elm-color module. The original source code can be found here.

The license of that package is produced below:

MIT License

Copyright 2018 Aaron VonderHaar

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Types

pub type Color =
  Colour

A representation of a colour that can be converted to RGBA or HSLA format.


pub opaque type Colour

Constants

pub const black: Colour

A Colour reprsenting the colour RGBA(0, 0, 0, 1.0)

pub const blue: Colour

A Colour reprsenting the colour RGBA(52, 101, 164, 1.0)

pub const brown: Colour

A Colour reprsenting the colour RGBA(193, 125, 17, 1.0)

pub const charcoal: Colour

A Colour reprsenting the colour RGBA(85, 87, 83, 1.0)

pub const dark_blue: Colour

A Colour reprsenting the colour RGBA(32, 74, 135, 1.0)

pub const dark_brown: Colour

A Colour reprsenting the colour RGBA(143, 89, 2, 1.0)

pub const dark_charcoal: Colour

A Colour reprsenting the colour RGBA(46, 52, 54, 1.0)

pub const dark_gray: Colour

A Colour reprsenting the colour RGBA(186, 189, 182, 1.0)

pub const dark_green: Colour

A Colour reprsenting the colour RGBA(78, 154, 6, 1.0)

pub const dark_grey: Colour

A Colour reprsenting the colour RGBA(186, 189, 182, 1.0)

pub const dark_orange: Colour

A Colour reprsenting the colour RGBA(206, 92, 0, 1.0)

pub const dark_purple: Colour

A Colour reprsenting the colour RGBA(92, 53, 102, 1.0)

pub const dark_red: Colour

A Colour reprsenting the colour RGBA(164, 0, 0, 1.0)

pub const dark_yellow: Colour

A Colour reprsenting the colour RGBA(196, 160, 0, 1.0)

pub const gray: Colour

A Colour reprsenting the colour RGBA(211, 215, 207, 1.0)

pub const green: Colour

A Colour reprsenting the colour RGBA(115, 210, 22, 1.0)

pub const grey: Colour

A Colour reprsenting the colour RGBA(211, 215, 207, 1.0)

pub const light_blue: Colour

A Colour reprsenting the colour RGBA(114, 159, 207, 1.0)

pub const light_brown: Colour

A Colour reprsenting the colour RGBA(233, 185, 110, 1.0)

pub const light_charcoal: Colour

A Colour reprsenting the colour RGBA(136, 138, 133, 1.0)

pub const light_gray: Colour

A Colour reprsenting the colour RGBA(238, 238, 236, 1.0)

pub const light_green: Colour

A Colour reprsenting the colour RGBA(138, 226, 52, 1.0)

pub const light_grey: Colour

A Colour reprsenting the colour RGBA(238, 238, 236, 1.0)

pub const light_orange: Colour

A Colour reprsenting the colour RGBA(252, 175, 62, 1.0)

pub const light_purple: Colour

A Colour reprsenting the colour RGBA(173, 127, 168, 1.0)

pub const light_red: Colour

A Colour reprsenting the colour RGBA(239, 41, 41, 1.0)

pub const light_yellow: Colour

A Colour reprsenting the colour RGBA(255, 233, 79, 1.0)

pub const orange: Colour

A Colour reprsenting the colour RGBA(245, 121, 0, 1.0)

pub const pink: Colour

A Colour reprsenting the colour RGBA(255, 175, 243, 1.0)

pub const purple: Colour

A Colour reprsenting the colour RGBA(117, 80, 123, 1.0)

pub const red: Colour

A Colour reprsenting the colour RGBA(204, 0, 0, 1.0)

pub const white: Colour

A Colour reprsenting the colour RGBA(255, 255, 255, 1.0)

pub const yellow: Colour

A Colour reprsenting the colour RGBA(237, 212, 0, 1.0)

Functions

pub fn decoder(
  json: Dynamic,
) -> Result(Colour, List(DecodeError))

Attempt to decode some Dynamic value into a Colour. Most often you’ll use this to decode some JSON.

pub fn encode(colour: Colour) -> Json

Encodes a Colour value as a Gleam Json value. You’ll need this if you want to send a Colour value over the network in a HTTP request or response, for example.

pub fn from_hsl(
  h hue: Float,
  s saturation: Float,
  l lightness: Float,
) -> Result(Colour, Nil)

Returns Result(Colour) created from the given HSL values.

Returns Error(Nil) if the supplied HSL values are greater than 1.0 or less than 0.0.

Example:
fn example() {
  assert Ok(red) = from_hsla(0.0, 1.0, 0.5)
}
pub fn from_hsla(
  h hue: Float,
  s saturation: Float,
  l lightness: Float,
  a alpha: Float,
) -> Result(Colour, Nil)

Returns Result(Colour) created from the given HSLA values.

Returns Error(Nil)f the supplied HSLA values are greater than 1.0 or less than 0.0.

Example:
fn example() {
  assert Ok(red_half_opacity) = from_hsla(0.0, 1.0, 0.5, 0.5)
}
pub fn from_rgb(
  r red: Float,
  g green: Float,
  b blue: Float,
) -> Result(Colour, Nil)

Returns Result(Colour) created from the given RGB values.

If the supplied RGB values are greater than 1.0 or less than 0.0 returns Error(Nil)

Example:
fn example() {
  assert Ok(red) = from_rgb(1.0, 0.0, 0.0)
}
pub fn from_rgb255(
  r red: Int,
  g green: Int,
  b blue: Int,
) -> Result(Colour, Nil)

Returns a Result(Colour) created from the given 8 bit RGB values.

Returns Error(Nil) if the supplied RGB values are greater than 255 or less than 0.

Example:
fn example() {
  assert Ok(red) = from_rgb255(255, 0, 0)
}
pub fn from_rgb_hex(hex: Int) -> Result(Colour, Nil)

Returns a Result(Colour) created from the given hex Int.

Returns Error(Nil) if the supplied hex `Int is greater than 0xffffff or less than 0x0.

Example:
fn example() {
  assert Ok(red) = from_rgb_hex(0xff0000)
}
pub fn from_rgb_hex_string(
  hex_string: String,
) -> Result(Colour, Nil)

Returns a Result(Colour) created from the given RGB hex String.

Returns Error(Nil) if the supplied hex String is invalid, or greater than "#ffffff" or less than “#0”`

Example:
fn example() {
  assert Ok(red) = from_rgb_hex_string("#ff0000")
}
pub fn from_rgba(
  r red: Float,
  g green: Float,
  b blue: Float,
  a alpha: Float,
) -> Result(Colour, Nil)

Returns Result(Colour) created from the given RGBA values.

Returns Error(Nil) if the supplied RGBA values are greater than 1.0 or less than 0.0.

Example:
fn example() {
  assert Ok(red_half_opacity) = from_rbga(1.0, 0.0, 0.0, 0.5)
}
pub fn from_rgba_hex(hex: Int) -> Result(Colour, Nil)

Returns a Result(Colour) created from the given hex Int.

Returns Error(Nil) if the supplied hex `Int is greater than 0xffffffff or less than 0x0.

Example:
fn example() {
  assert Ok(red_half_opacity) = from_rgba_hex(0xff00007f)
}
pub fn from_rgba_hex_string(
  hex_string: String,
) -> Result(Colour, Nil)

Returns a Result(Colour) created from the given RGBA hex String.

Returns Error(Nil) if the supplied hex String is invalid, or greater than "#ffffffff" or less than “#0”`

Example:
fn example() {
  assert Ok(red_half_opacity) = from_rgba_hex_string("#ff00007f")
}
pub fn to_css_rgba_string(colour: Colour) -> String

Returns an rgba formatted CSS String created from the given Colour.

Example:
fn example() {
  assert Ok(red) = from_rgb255(255, 0, 0)
  let css_red = to_css_rgba_string(red)
}
pub fn to_hsla(colour: Colour) -> #(Float, Float, Float, Float)

Returns #(Float, Float, Float, Float) representing the given Colour’s H, S, L, and A values respectively.

Example:
fn example() {
  assert Ok(red) = from_rgb255(255, 0, 0)
  let #(h, s, l, a) = to_hsla(red)
}
pub fn to_rgb_hex(colour: Colour) -> Int

Returns a rgb hex Int created from the given Colour.

Example:
fn example() {
  assert Ok(red) = from_rgba(255, 0, 0)
  let red_hex_int = to_rgb_hex(red)
}
pub fn to_rgb_hex_string(colour: Colour) -> String

Returns an rgb hex formatted String created from the given Colour.

Example:
fn example() {
  assert Ok(red) = from_rgba(255, 0, 0)
  let red_hex = to_rgb_hex_string(red)
}
pub fn to_rgba(colour: Colour) -> #(Float, Float, Float, Float)

Returns #(Float, Float, Float, Float) representing the given Colour’s R, G, B, and A values respectively.

Example:
fn example() {
  assert Ok(red) = from_rgb255(255, 0, 0)
  let #(r, g, b, a) = to_rgba(red)
}
pub fn to_rgba_hex(colour: Colour) -> Int

Returns an hex Int created from the given Colour.

Example:
fn example() {
  assert Ok(red) = from_rgba(1.0, 0.0, 0.0, 1.0)
  let red_hex_int = to_rgba_hex(red)
}
pub fn to_rgba_hex_string(colour: Colour) -> String

Returns an rgba hex formatted String created from the given Colour.

Example:
fn example() {
  assert Ok(red) = from_rgba(1.0, 0.0, 0.0, 1.0)
  let red_hex = to_rgba_hex_string(red)
}
Search Document