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 = Rgba(r: 0.0, g: 0.0, b: 0.0, a: 1.0)

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

pub const blue: Colour = Rgba(
  r: 0.20392156862745098,
  g: 0.396078431372549,
  b: 0.6431372549019608,
  a: 1.0,
)

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

pub const brown: Colour = Rgba(
  r: 0.7568627450980392,
  g: 0.49019607843137253,
  b: 0.06666666666666667,
  a: 1.0,
)

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

pub const charcoal: Colour = Rgba(
  r: 0.3333333333333333,
  g: 0.3411764705882353,
  b: 0.3254901960784314,
  a: 1.0,
)

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

pub const dark_blue: Colour = Rgba(
  r: 0.12549019607843137,
  g: 0.2901960784313726,
  b: 0.5294117647058824,
  a: 1.0,
)

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

pub const dark_brown: Colour = Rgba(
  r: 0.5607843137254902,
  g: 0.34901960784313724,
  b: 0.00784313725490196,
  a: 1.0,
)

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

pub const dark_charcoal: Colour = Rgba(
  r: 0.1803921568627451,
  g: 0.20392156862745098,
  b: 0.21176470588235294,
  a: 1.0,
)

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

pub const dark_gray: Colour = Rgba(
  r: 0.7294117647058823,
  g: 0.7411764705882353,
  b: 0.7137254901960784,
  a: 1.0,
)

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

pub const dark_green: Colour = Rgba(
  r: 0.3058823529411765,
  g: 0.6039215686274509,
  b: 0.023529411764705882,
  a: 1.0,
)

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

pub const dark_grey: Colour = Rgba(
  r: 0.7294117647058823,
  g: 0.7411764705882353,
  b: 0.7137254901960784,
  a: 1.0,
)

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

pub const dark_orange: Colour = Rgba(
  r: 0.807843137254902,
  g: 0.3607843137254902,
  b: 0.0,
  a: 1.0,
)

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

pub const dark_purple: Colour = Rgba(
  r: 0.3607843137254902,
  g: 0.20784313725490197,
  b: 0.4,
  a: 1.0,
)

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

pub const dark_red: Colour = Rgba(
  r: 0.6431372549019608,
  g: 0.0,
  b: 0.0,
  a: 1.0,
)

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

pub const dark_yellow: Colour = Rgba(
  r: 0.7686274509803922,
  g: 0.6274509803921569,
  b: 0.0,
  a: 1.0,
)

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

pub const gray: Colour = Rgba(
  r: 0.8274509803921568,
  g: 0.8431372549019608,
  b: 0.8117647058823529,
  a: 1.0,
)

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

pub const green: Colour = Rgba(
  r: 0.45098039215686275,
  g: 0.8235294117647058,
  b: 0.08627450980392157,
  a: 1.0,
)

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

pub const grey: Colour = Rgba(
  r: 0.8274509803921568,
  g: 0.8431372549019608,
  b: 0.8117647058823529,
  a: 1.0,
)

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

pub const light_blue: Colour = Rgba(
  r: 0.4470588235294118,
  g: 0.6235294117647059,
  b: 0.8117647058823529,
  a: 1.0,
)

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

pub const light_brown: Colour = Rgba(
  r: 0.9137254901960784,
  g: 0.7254901960784313,
  b: 0.43137254901960786,
  a: 1.0,
)

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

pub const light_charcoal: Colour = Rgba(
  r: 0.5333333333333333,
  g: 0.5411764705882353,
  b: 0.5215686274509804,
  a: 1.0,
)

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

pub const light_gray: Colour = Rgba(
  r: 0.9333333333333333,
  g: 0.9333333333333333,
  b: 0.9254901960784314,
  a: 1.0,
)

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

pub const light_green: Colour = Rgba(
  r: 0.5411764705882353,
  g: 0.8862745098039215,
  b: 0.20392156862745098,
  a: 1.0,
)

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

pub const light_grey: Colour = Rgba(
  r: 0.9333333333333333,
  g: 0.9333333333333333,
  b: 0.9254901960784314,
  a: 1.0,
)

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

pub const light_orange: Colour = Rgba(
  r: 0.9882352941176471,
  g: 0.6862745098039216,
  b: 0.24313725490196078,
  a: 1.0,
)

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

pub const light_purple: Colour = Rgba(
  r: 0.6784313725490196,
  g: 0.4980392156862745,
  b: 0.6588235294117647,
  a: 1.0,
)

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

pub const light_red: Colour = Rgba(
  r: 0.9372549019607843,
  g: 0.1607843137254902,
  b: 0.1607843137254902,
  a: 1.0,
)

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

pub const light_yellow: Colour = Rgba(
  r: 1.0,
  g: 0.9137254901960784,
  b: 0.30980392156862746,
  a: 1.0,
)

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

pub const orange: Colour = Rgba(
  r: 0.9607843137254902,
  g: 0.4745098039215686,
  b: 0.0,
  a: 1.0,
)

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

pub const pink: Colour = Rgba(
  r: 1.0,
  g: 0.6862745098039216,
  b: 0.9529411764705882,
  a: 1.0,
)

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

pub const purple: Colour = Rgba(
  r: 0.4588235294117647,
  g: 0.3137254901960784,
  b: 0.4823529411764706,
  a: 1.0,
)

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

pub const red: Colour = Rgba(r: 0.8, g: 0.0, b: 0.0, a: 1.0)

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

pub const white: Colour = Rgba(r: 1.0, g: 1.0, b: 1.0, a: 1.0)

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

pub const yellow: Colour = Rgba(
  r: 0.9294117647058824,
  g: 0.8313725490196079,
  b: 0.0,
  a: 1.0,
)

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