Scenic.Color (Scenic v0.11.0-beta.0) View Source

APIs to create and work with colors.

Colors are used in multiple places in Scenic. Fills and Strokes of a single color are quite common.

Data Format

There are multiple ways to define colors.

The native format of color on modern computers is RGBA. This is four channels including Red, Green, Blue, and Alpha. Alpha indicates transparency, and is used to blend the color being applied at any given location with the color that is already there.

Most of the time, you will use one of the pre-defined named colors from the Named Colors table. However, there are times when you want to work with other color formats ranging from simple grayscale to rgb to hsl.

The following formats are all supported by the Scenic.Color module. The values of r, g, b, and a are integers between 0 and 255. For HSL and HSV, h is a float between 0 and 360, while the s, v and l values are floats between 0 and 100.

FormatImplicitExplicit
Named ColornaSee the Named Color Table
Grayscaleg{:g, g}
Gray, Alpha{g, a}{:g, {g, a}}
Red, Green, Blue{r, g, b}{:rgb, {r, g, b}}
Red, Green, Blue, Alpha{r, g, b, a}{:rgba, {r, g, b, a}}
Hue, Saturation, Valuena{:hsv, {h, s, v}}
Hue, Saturation, Lightnessna{:hsl, {h, s, l}}

Named Colors

The simplest is to used a named color (see the table below). Named colors are simply referred to by an atom, which is their name. Named colors are opaque by default. I failed to figure out how to show a table with colored cells in exdoc. So this is a list of all the color names. I'll eventually add a link to a page that shows them visually.

[:alice_blue, :antique_white, :aqua, :aquamarine, :azure, :beige, :bisque, :black, :blanched_almond, :blue, :blue_violet, :brown, :burly_wood, :cadet_blue, :chartreuse, :chocolate, :coral, :cornflower_blue, :cornsilk, :crimson, :cyan, :dark_blue, :dark_cyan, :dark_golden_rod, :dark_gray, :dark_green, :dark_grey, :dark_khaki, :dark_magenta, :dark_olive_green, :dark_orange, :dark_orchid, :dark_red, :dark_salmon, :dark_sea_green, :dark_slate_blue, :dark_slate_gray, :dark_slate_grey, :dark_turquoise, :dark_violet, :deep_pink, :deep_sky_blue, :dim_gray, :dim_grey, :dodger_blue, :fire_brick, :floral_white, :forest_green, :fuchsia, :gainsboro, :ghost_white, :gold, :golden_rod, :gray, :green, :green_yellow, :grey, :honey_dew, :hot_pink, :indian_red, :indigo, :ivory, :khaki, :lavender, :lavender_blush, :lawn_green, :lemon_chiffon, :light_blue, :light_coral, :light_cyan, :light_golden_rod, :light_golden_rod_yellow, :light_gray, :light_green, :light_grey, :light_pink, :light_salmon, :light_sea_green, :light_sky_blue, :light_slate_gray, :light_slate_grey, :light_steel_blue, :light_yellow, :lime, :lime_green, :linen, :magenta, :maroon, :medium_aqua_marine, :medium_blue, :medium_orchid, :medium_purple, :medium_sea_green, :medium_slate_blue, :medium_spring_green, :medium_turquoise, :medium_violet_red, :midnight_blue, :mint_cream, :misty_rose, :moccasin, :navajo_white, :navy, :old_lace, :olive, :olive_drab, :orange, :orange_red, :orchid, :pale_golden_rod, :pale_green, :pale_turquoise, :pale_violet_red, :papaya_whip, :peach_puff, :peru, :pink, :plum, :powder_blue, :purple, :rebecca_purple, :red, :rosy_brown, :royal_blue, :saddle_brown, :salmon, :sandy_brown, :sea_green, :sea_shell, :sienna, :silver, :sky_blue, :slate_blue, :slate_gray, :slate_grey, :snow, :spring_green, :steel_blue, :tan, :teal, :thistle, :tomato, :turquoise, :violet, :wheat, :white, :white_smoke, :yellow, :yellow_green]

Additional Named Colors

NameValue
:clear{0x80, 0x80, 0x80, 0x00}
:transparent{0x80, 0x80, 0x80, 0x00}

Converting Between Color Formats

By using the functions to_g, to_ga, to_rgb, to_rgb, to_hsl, and to_hsv you can convert between any implicit or explicit color type to any explicit color type.

Link to this section Summary

Functions

Return map of all named colors and their values

Convert a specified color to G format (just grayscale)

Convert a specified color to GA format

Convert a color to the HSL color space

Convert a color to the HSV color space

Convert a specified color to RGB format

Convert a specified color to RGBA format

Link to this section Types

Specs

explicit() :: g() | ga() | rgb() | rgba() | hsl() | hsv()

Specs

g() :: {:color_g, grey :: integer()}

Specs

ga() :: {:color_ga, {grey :: integer(), alpha :: integer()}}

Specs

hsl() ::
  {:color_hsl, {hue :: number(), saturation :: number(), lightness :: number()}}

Specs

hsv() ::
  {:color_hsv, {hue :: number(), saturation :: number(), value :: number()}}

Specs

implicit() ::
  atom()
  | {name :: atom(), a :: integer()}
  | (gray :: integer())
  | {gray :: integer(), alpha :: integer()}
  | {red :: integer(), green :: integer(), blue :: integer()}
  | {red :: integer(), green :: integer(), blue :: integer(),
     alpha :: integer()}

Specs

rgb() :: {:color_rgb, {red :: integer(), green :: integer(), blue :: integer()}}

Specs

rgba() ::
  {:color_rgba,
   {red :: integer(), green :: integer(), blue :: integer(), alpha :: integer()}}

Specs

t() :: implicit() | explicit()

Link to this section Functions

Return map of all named colors and their values

Specs

to_g(color :: t()) :: g()

Convert a specified color to G format (just grayscale)

This is a lossy conversion and will lose any color information other than the gray level.

Specs

to_ga(color :: t()) :: ga()

Convert a specified color to GA format

Specs

to_hsl(color :: t()) :: hsl()

Convert a color to the HSL color space

Specs

to_hsv(color :: t()) :: hsv()

Convert a color to the HSV color space

Specs

to_rgb(color :: t()) :: rgb()

Convert a specified color to RGB format

Specs

to_rgba(color :: t()) :: rgba()

Convert a specified color to RGBA format