phollyer/elm-motion/3.0.2

NOTE: the latest version is 3.0.4

Anim.Extra.Color

Color manipulation and conversion utilities for the shared Color type.

Use these functions to create, transform, and convert colors in various formats, then use them in animations.

Color Type

type alias Color =
Color

Color used in animations.

Color Constructors

Use these functions to create colors.

hex : String -> Maybe Color

Create a hex color from a string.

hex "#ff0000" -- Red


hex "#f00" -- Red (shorthand)


hex "ff0000" -- Red (without #)

Invalid hex strings will return Nothing.

rgb : Int -> Int -> Int -> Color

Create an RGB color from individual components.

rgb 255 0 0 -- Red
rgba : Int -> Int -> Int -> Float -> Color

Create an RGBA color from individual components.

rgba 255 0 0 0.5 -- Semi-transparent red
hsl : Float -> Float -> Float -> Color

Create an HSL color from individual components.

  • h (hue): 0-360 degrees
  • s (saturation): 0-100 percent
  • l (lightness): 0-100 percent
hsl 0 100 50 -- Red
hsla : Float -> Float -> Float -> Float -> Color

Create an HSLA color from individual components.

  • h (hue): 0-360 degrees
  • s (saturation): 0-100 percent
  • l (lightness): 0-100 percent
  • a (alpha): 0-1
hsla 0 100 50 0.5 -- Semi-transparent red
elmColor : Color -> Color

Create a Color from an elm-color Color value.

elmColor Color.red

Color Transformations

Hex Colors

fromHex : String -> Maybe Color

Create a color from a hex string.

fromHex "#ff0000" -- Just Red


fromHex "#f00" -- Just Red (shorthand)


fromHex "invalid" -- Nothing

Invalid hex strings will return Nothing.

toHex : Color -> String

Convert a Color to a hex string .

RGB Colors

{ r : Int
, g : Int
, b : Int
}
-> Color

Create a Color from RGB components.

fromRgb { r = 255, g = 0, b = 0 } -- Red
{ r : Int
, g : Int
, b : Int
, a : Float
}
-> Color

Create a Color from RGBA components.

fromRgba { r = 255, g = 0, b = 0, a = 0.5 } -- Semi-transparent red
Color
-> { r : Int
, g : Int
, b : Int
}

Convert a Color to an RGB color record.

Color
-> { r : Int
, g : Int
, b : Int
, a : Float
}

Convert a Color to an RGBA color record.

HSL Colors

{ h : Float
, s : Float
, l : Float
}
-> Color

Create a Color from HSL components.

  • h (hue): 0-360 degrees
  • s (saturation): 0-100 percent
  • l (lightness): 0-100 percent
fromHsl { h = 0, s = 100, l = 50 } -- Red
{ h : Float
, s : Float
, l : Float
, a : Float
}
-> Color

Create a Color from HSLA components.

  • h (hue): 0-360 degrees
  • s (saturation): 0-100 percent
  • l (lightness): 0-100 percent
  • a (alpha): 0-1
fromHsla { h = 0, s = 100, l = 50, a = 0.5 } -- Semi-transparent red
Color
-> { h : Float
, s : Float
, l : Float
}

Convert a Color to an HSL color record.

Color
-> { h : Float
, s : Float
, l : Float
, a : Float
}

Convert a Color to an HSLA color record.

Elm Color

Use colors from the avh4/elm-color package.

fromElmColor : Color -> Color

Create a Color from an elm-color Color value.

import Color


fromElmColor Color.red
toElmColor : Color -> Color

Convert a Color to an elm-color Color value.

Parsing

fromString : String -> Maybe Color

Parse a color from various string formats.

Supports:

  • Hex: "#ff0000", "#f00", "ff0000", "f00"

  • RGB: "rgb(255, 0, 0)"

  • RGBA: "rgba(255, 0, 0, 0.5)"

  • HSL: "hsl(0, 100%, 50%)"

  • HSLA: "hsla(0, 100%, 50%, 0.5)"

fromString "#ff0000" -- Just red

fromString "rgb(255, 0, 0)" -- Just red

fromString "invalid" -- Nothing

Manipulation

setAlpha : Float -> Color -> Color

Set the alpha (transparency) value of a color.

-- Semi-transparent red
setAlpha 0.5 <|
    fromRgb { r = 255, g = 0, b = 0 }
brighten : Float -> Color -> Color

Increase the lightness of a color.

brighten 0.2 <|
    fromRgb { r = 100, g = 100, b = 100 } -- Lighter gray
darken : Float -> Color -> Color

Decrease the lightness of a color.

darken 0.2 <|
    fromRgb { r = 100, g = 100, b = 100 } -- Darker gray
saturate : Float -> Color -> Color

Increase the saturation of a color.

saturate 0.2 <|
    fromHsl {h = 0, s = 50, l = 50} -- More saturated red
desaturate : Float -> Color -> Color

Decrease the saturation of a color.

desaturate 0.2 <|
    fromHsl {h = 0, s = 50, l = 50} -- Less saturated red

Common Colors

transparent : Color

Fully transparent white.

black : Color
white : Color
red : Color
green : Color
blue : Color