Color.Convert

#Convert Convert colors to differnt string formats and hexadecimal strings to colors.


colorToCssRgb : Color.Color -> String.String

Converts a color to an css rgb string.

colorToCssRgb (rgb 255 0 0) -- "rgb(255, 0, 0)"

colorToCssRgba : Color.Color -> String.String

Converts a color to an css rgba string.

colorToCssRgba (rgba 255 0 0 0.5) -- "rgba(255, 0, 0, 0.5)"

colorToCssHsl : Color.Color -> String.String

Converts a color to an css hsl string.

colorToCssHsl (hsl 1 1 0.5) -- "hsl(1, 1, 0.5)"

colorToCssHsla : Color.Color -> String.String

Converts a color to an css hsla string.

colorToCssHsla (hsla 1 1 0.5 1) -- "hsla(56, 100%, 50%, 1)"

colorToHex : Color.Color -> String.String

Converts a color to a hexadecimal string.

colorToHex (rgb  255 0 0)     -- "#ff0000"
colorToHex (rgba 255 0 0 1.0) -- "#ff0000"
colorToHex (rgba 255 0 0 0.5) -- "#ff0000"
colorToHex (rgba 255 0 0 0.0) -- "#ff0000"

If you want support for colors with alpha transparency, either use colorToCssRgba or colorToHexWithAlpha.


colorToHexWithAlpha : Color.Color -> String.String

Converts a color to a hexadecimal string.

If the color has alpha transparency different from 1, it will use the #RRGGBBAA format. Note that the support for that is (as of March 2018) missing on IE, Edge and some mobile browsers. It may be better to use colorToCssRgba, which has excellent support.

colorToHexWithAlpha (rgb  255 0 0)     -- "#ff0000"
colorToHexWithAlpha (rgba 255 0 0 1.0) -- "#ff0000"
colorToHexWithAlpha (rgba 255 0 0 0.5) -- "#ff000080"
colorToHexWithAlpha (rgba 255 0 0 0.0) -- "#ff000000"


hexToColor : String.String -> Result.Result String.String Color.Color

Converts a string to Maybe of color.

hexToColor "#ff0000" -- "Ok (RGB 255 0 0)"
hexToColor "#f00" -- "Ok (RGB 255 0 0)"
hexToColor "#ff000080" -- "Ok (RGBA 255 0 0 0.5)"
hexToColor "ff0000" -- "Ok (RGB 255 0 0)"
hexToColor "f00" -- "Ok (RGB 255 0 0)"
hexToColor "ff000080" -- "Ok (RGBA 255 0 0 0.5)"
hexToColor "1234" -- "Err \"Parsing hex regex failed\""

colorToLab : Color.Color -> { l : Basics.Float, a : Basics.Float, b : Basics.Float }

Convert color to CIELAB- color space


labToColor : { l : Basics.Float, a : Basics.Float, b : Basics.Float } -> Color.Color

Convert a color in CIELAB- color space to Elm Color