Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for library themes #252

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
159 changes: 4 additions & 155 deletions lib/scenic/color.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,157 +4,7 @@
#

defmodule Scenic.Color do
@named_colors %{
alice_blue: {0xF0, 0xF8, 0xFF},
antique_white: {0xFA, 0xEB, 0xD7},
aqua: {0x00, 0xFF, 0xFF},
aquamarine: {0x7F, 0xFF, 0xD4},
azure: {0xF0, 0xFF, 0xFF},
beige: {0xF5, 0xF5, 0xDC},
bisque: {0xFF, 0xE4, 0xC4},
black: {0x00, 0x00, 0x00},
blanched_almond: {0xFF, 0xEB, 0xCD},
blue: {0x00, 0x00, 0xFF},
blue_violet: {0x8A, 0x2B, 0xE2},
brown: {0xA5, 0x2A, 0x2A},
burly_wood: {0xDE, 0xB8, 0x87},
cadet_blue: {0x5F, 0x9E, 0xA0},
chartreuse: {0x7F, 0xFF, 0x00},
chocolate: {0xD2, 0x69, 0x1E},
coral: {0xFF, 0x7F, 0x50},
cornflower_blue: {0x64, 0x95, 0xED},
cornsilk: {0xFF, 0xF8, 0xDC},
crimson: {0xDC, 0x14, 0x3C},
cyan: {0x00, 0xFF, 0xFF},
dark_blue: {0x00, 0x00, 0x8B},
dark_cyan: {0x00, 0x8B, 0x8B},
dark_golden_rod: {0xB8, 0x86, 0x0B},
dark_gray: {0xA9, 0xA9, 0xA9},
dark_grey: {0xA9, 0xA9, 0xA9},
dark_green: {0x00, 0x64, 0x00},
dark_khaki: {0xBD, 0xB7, 0x6B},
dark_magenta: {0x8B, 0x00, 0x8B},
dark_olive_green: {0x55, 0x6B, 0x2F},
dark_orange: {0xFF, 0x8C, 0x00},
dark_orchid: {0x99, 0x32, 0xCC},
dark_red: {0x8B, 0x00, 0x00},
dark_salmon: {0xE9, 0x96, 0x7A},
dark_sea_green: {0x8F, 0xBC, 0x8F},
dark_slate_blue: {0x48, 0x3D, 0x8B},
dark_slate_gray: {0x2F, 0x4F, 0x4F},
dark_slate_grey: {0x2F, 0x4F, 0x4F},
dark_turquoise: {0x00, 0xCE, 0xD1},
dark_violet: {0x94, 0x00, 0xD3},
deep_pink: {0xFF, 0x14, 0x93},
deep_sky_blue: {0x00, 0xBF, 0xFF},
dim_gray: {0x69, 0x69, 0x69},
dim_grey: {0x69, 0x69, 0x69},
dodger_blue: {0x1E, 0x90, 0xFF},
fire_brick: {0xB2, 0x22, 0x22},
floral_white: {0xFF, 0xFA, 0xF0},
forest_green: {0x22, 0x8B, 0x22},
fuchsia: {0xFF, 0x00, 0xFF},
gainsboro: {0xDC, 0xDC, 0xDC},
ghost_white: {0xF8, 0xF8, 0xFF},
gold: {0xFF, 0xD7, 0x00},
golden_rod: {0xDA, 0xA5, 0x20},
gray: {0x80, 0x80, 0x80},
grey: {0x80, 0x80, 0x80},
green: {0x00, 0x80, 0x00},
green_yellow: {0xAD, 0xFF, 0x2F},
honey_dew: {0xF0, 0xFF, 0xF0},
hot_pink: {0xFF, 0x69, 0xB4},
indian_red: {0xCD, 0x5C, 0x5C},
indigo: {0x4B, 0x00, 0x82},
ivory: {0xFF, 0xFF, 0xF0},
khaki: {0xF0, 0xE6, 0x8C},
lavender: {0xE6, 0xE6, 0xFA},
lavender_blush: {0xFF, 0xF0, 0xF5},
lawn_green: {0x7C, 0xFC, 0x00},
lemon_chiffon: {0xFF, 0xFA, 0xCD},
light_blue: {0xAD, 0xD8, 0xE6},
light_coral: {0xF0, 0x80, 0x80},
light_cyan: {0xE0, 0xFF, 0xFF},
light_golden_rod: {0xFA, 0xFA, 0xD2},
light_golden_rod_yellow: {0xFA, 0xFA, 0xD2},
light_gray: {0xD3, 0xD3, 0xD3},
light_grey: {0xD3, 0xD3, 0xD3},
light_green: {0x90, 0xEE, 0x90},
light_pink: {0xFF, 0xB6, 0xC1},
light_salmon: {0xFF, 0xA0, 0x7A},
light_sea_green: {0x20, 0xB2, 0xAA},
light_sky_blue: {0x87, 0xCE, 0xFA},
light_slate_gray: {0x77, 0x88, 0x99},
light_slate_grey: {0x77, 0x88, 0x99},
light_steel_blue: {0xB0, 0xC4, 0xDE},
light_yellow: {0xFF, 0xFF, 0xE0},
lime: {0x00, 0xFF, 0x00},
lime_green: {0x32, 0xCD, 0x32},
linen: {0xFA, 0xF0, 0xE6},
magenta: {0xFF, 0x00, 0xFF},
maroon: {0x80, 0x00, 0x00},
medium_aqua_marine: {0x66, 0xCD, 0xAA},
medium_blue: {0x00, 0x00, 0xCD},
medium_orchid: {0xBA, 0x55, 0xD3},
medium_purple: {0x93, 0x70, 0xDB},
medium_sea_green: {0x3C, 0xB3, 0x71},
medium_slate_blue: {0x7B, 0x68, 0xEE},
medium_spring_green: {0x00, 0xFA, 0x9A},
medium_turquoise: {0x48, 0xD1, 0xCC},
medium_violet_red: {0xC7, 0x15, 0x85},
midnight_blue: {0x19, 0x19, 0x70},
mint_cream: {0xF5, 0xFF, 0xFA},
misty_rose: {0xFF, 0xE4, 0xE1},
moccasin: {0xFF, 0xE4, 0xB5},
navajo_white: {0xFF, 0xDE, 0xAD},
navy: {0x00, 0x00, 0x80},
old_lace: {0xFD, 0xF5, 0xE6},
olive: {0x80, 0x80, 0x00},
olive_drab: {0x6B, 0x8E, 0x23},
orange: {0xFF, 0xA5, 0x00},
orange_red: {0xFF, 0x45, 0x00},
orchid: {0xDA, 0x70, 0xD6},
pale_golden_rod: {0xEE, 0xE8, 0xAA},
pale_green: {0x98, 0xFB, 0x98},
pale_turquoise: {0xAF, 0xEE, 0xEE},
pale_violet_red: {0xDB, 0x70, 0x93},
papaya_whip: {0xFF, 0xEF, 0xD5},
peach_puff: {0xFF, 0xDA, 0xB9},
peru: {0xCD, 0x85, 0x3F},
pink: {0xFF, 0xC0, 0xCB},
plum: {0xDD, 0xA0, 0xDD},
powder_blue: {0xB0, 0xE0, 0xE6},
purple: {0x80, 0x00, 0x80},
rebecca_purple: {0x66, 0x33, 0x99},
red: {0xFF, 0x00, 0x00},
rosy_brown: {0xBC, 0x8F, 0x8F},
royal_blue: {0x41, 0x69, 0xE1},
saddle_brown: {0x8B, 0x45, 0x13},
salmon: {0xFA, 0x80, 0x72},
sandy_brown: {0xF4, 0xA4, 0x60},
sea_green: {0x2E, 0x8B, 0x57},
sea_shell: {0xFF, 0xF5, 0xEE},
sienna: {0xA0, 0x52, 0x2D},
silver: {0xC0, 0xC0, 0xC0},
sky_blue: {0x87, 0xCE, 0xEB},
slate_blue: {0x6A, 0x5A, 0xCD},
slate_gray: {0x70, 0x80, 0x90},
slate_grey: {0x70, 0x80, 0x90},
snow: {0xFF, 0xFA, 0xFA},
spring_green: {0x00, 0xFF, 0x7F},
steel_blue: {0x46, 0x82, 0xB4},
tan: {0xD2, 0xB4, 0x8C},
teal: {0x00, 0x80, 0x80},
thistle: {0xD8, 0xBF, 0xD8},
tomato: {0xFF, 0x63, 0x47},
turquoise: {0x40, 0xE0, 0xD0},
violet: {0xEE, 0x82, 0xEE},
wheat: {0xF5, 0xDE, 0xB3},
white: {0xFF, 0xFF, 0xFF},
white_smoke: {0xF5, 0xF5, 0xF5},
yellow: {0xFF, 0xFF, 0x00},
yellow_green: {0x9A, 0xCD, 0x32}
}
alias Scenic.Themes

@moduledoc """
APIs to create and work with colors.
Expand All @@ -173,7 +23,7 @@ defmodule Scenic.Color do

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.
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.
Expand All @@ -199,8 +49,6 @@ defmodule Scenic.Color do
a list of all the color names. I'll eventually add a link to a page that shows them
visually.

#{inspect(Enum.map(@named_colors, fn {k, _v} -> k end) |> Enum.sort(), limit: :infinity, pretty: true)}


## Additional Named Colors

Expand Down Expand Up @@ -613,10 +461,11 @@ defmodule Scenic.Color do
end

# --------------------------------------------------------
@spec named :: map
@doc """
Return map of all named colors and their values
"""
def named(), do: @named_colors
def named(), do: Themes.get_palette()

# --------------------------------------------------------
# @doc """
Expand Down
19 changes: 11 additions & 8 deletions lib/scenic/component/button.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ defmodule Scenic.Component.Button do

alias Scenic.Graph
alias Scenic.Scene
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
alias Scenic.Assets.Static

import Scenic.Primitives, only: [{:rrect, 3}, {:text, 3}, {:update_opts, 2}]
Expand Down Expand Up @@ -164,12 +164,15 @@ defmodule Scenic.Component.Button do
# theme is passed in as an inherited style
theme =
case opts[:theme] do
nil -> Theme.preset(:primary)
:dark -> Theme.preset(:primary)
:light -> Theme.preset(:primary)
theme -> theme
nil -> Themes.preset({:scenic, :primary})
{:scenic, :dark} -> Themes.preset({:scenic, :primary})
{:scenic, :light} -> Themes.preset({:scenic, :primary})
theme ->
case Themes.normalize(theme) do
nil -> Themes.preset({:scenic, :primary})
theme -> theme
end
end
|> Theme.normalize()

# font related info
font = Keyword.get(styles, :font, @default_font)
Expand Down Expand Up @@ -278,11 +281,11 @@ defmodule Scenic.Component.Button do
)
end

defp do_special_theme_outline(graph, :dark, border) do
defp do_special_theme_outline(graph, {:scenic, :dark}, border) do
Graph.modify(graph, :btn, &update_opts(&1, stroke: {1, border}))
end

Copy link
Contributor Author

@vacarsu vacarsu Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about these, It doesn't seem like they will ever match. I left them as is since I was already making such a sweeping change.

defp do_special_theme_outline(graph, :light, border) do
defp do_special_theme_outline(graph, {:scenic, :light}, border) do
Graph.modify(graph, :btn, &update_opts(&1, stroke: {1, border}))
end

Expand Down
4 changes: 2 additions & 2 deletions lib/scenic/component/input/caret.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Scenic.Component.Input.Caret do
]

alias Scenic.Graph
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes

@width 2
@inset_v 4
Expand Down Expand Up @@ -86,7 +86,7 @@ defmodule Scenic.Component.Input.Caret do
case opts[:color] do
nil ->
opts[:theme]
|> Theme.normalize()
|> Themes.normalize()
|> Map.get(:highlight)

c ->
Expand Down
6 changes: 3 additions & 3 deletions lib/scenic/component/input/checkbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Scenic.Component.Input.Checkbox do
alias Scenic.Graph
alias Scenic.Scene
alias Scenic.Primitive
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
alias Scenic.Script
alias Scenic.Assets.Static

Expand Down Expand Up @@ -95,8 +95,8 @@ defmodule Scenic.Component.Input.Checkbox do

# theme is passed in as an inherited style
theme =
(opts[:theme] || Theme.preset(:dark))
|> Theme.normalize()
(opts[:theme] || Themes.preset({:scenic, :dark}))
|> Themes.normalize()

# font related info
{:ok, {Static.Font, fm}} = Static.meta(@default_font)
Expand Down
6 changes: 3 additions & 3 deletions lib/scenic/component/input/dropdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Scenic.Component.Input.Dropdown do

alias Scenic.Graph
alias Scenic.Scene
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
import Scenic.Primitives
alias Scenic.Assets.Static

Expand Down Expand Up @@ -188,8 +188,8 @@ defmodule Scenic.Component.Input.Dropdown do

# theme is passed in as an inherited style
theme =
(opts[:theme] || Theme.preset(:dark))
|> Theme.normalize()
(opts[:theme] || Themes.preset({:scenic, :dark}))
|> Themes.normalize()

# font related info
{:ok, {Static.Font, fm}} = Static.meta(@default_font)
Expand Down
6 changes: 3 additions & 3 deletions lib/scenic/component/input/radio_button.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Scenic.Component.Input.RadioButton do
alias Scenic.Scene
alias Scenic.Graph
alias Scenic.Primitive
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
alias Scenic.Assets.Static

require Logger
Expand Down Expand Up @@ -70,8 +70,8 @@ defmodule Scenic.Component.Input.RadioButton do
def init(scene, {text, id, checked?}, opts) do
# theme is passed in as an inherited style
theme =
(opts[:theme] || Theme.preset(:dark))
|> Theme.normalize()
(opts[:theme] || Themes.preset({:scenic, :dark}))
|> Themes.normalize()

# font related info
{:ok, {Static.Font, fm}} = Static.meta(@default_font)
Expand Down
6 changes: 3 additions & 3 deletions lib/scenic/component/input/slider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule Scenic.Component.Input.Slider do
use Scenic.Component, has_children: false

alias Scenic.Graph
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
import Scenic.Primitives, only: [{:rect, 3}, {:line, 3}, {:rrect, 3}, {:update_opts, 2}]

require Logger
Expand Down Expand Up @@ -192,8 +192,8 @@ defmodule Scenic.Component.Input.Slider do

# theme is passed in as an inherited style
theme =
(opts[:theme] || Theme.preset(:primary))
|> Theme.normalize()
(opts[:theme] || Themes.preset({:scenic, :dark}))
|> Themes.normalize()

# get button specific styles
width = opts[:width] || @default_width
Expand Down
6 changes: 3 additions & 3 deletions lib/scenic/component/input/text_field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Scenic.Component.Input.TextField do

alias Scenic.Graph
alias Scenic.Component.Input.Caret
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
# alias Scenic.Assets.Static

require Logger
Expand Down Expand Up @@ -138,8 +138,8 @@ defmodule Scenic.Component.Input.TextField do

# theme is passed in as an inherited style
theme =
(opts[:theme] || Theme.preset(:dark))
|> Theme.normalize()
(opts[:theme] || Themes.preset({:scenic, :dark}))
|> Themes.normalize()

# get the text_field specific opts
hint = opts[:hint] || @default_hint
Expand Down
4 changes: 2 additions & 2 deletions lib/scenic/component/input/toggle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule Scenic.Component.Input.Toggle do
alias Scenic.Graph
alias Scenic.Primitive
alias Scenic.Primitive.Group
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
alias Scenic.ViewPort

import Scenic.Primitives
Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule Scenic.Component.Input.Toggle do
# theme is passed in as an inherited style
theme =
opts[:theme]
|> Theme.normalize()
|> Themes.normalize()

# get toggle specific opts
thumb_radius = Keyword.get(opts, :thumb_radius, @default_thumb_radius)
Expand Down
4 changes: 2 additions & 2 deletions lib/scenic/graph/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Scenic.Graph.Compiler do
alias Scenic.Primitive
alias Scenic.Graph
alias Scenic.Color
alias Scenic.Primitive.Style.Theme
alias Scenic.Themes
alias Scenic.Graph.Compiler

# import IEx
Expand Down Expand Up @@ -271,7 +271,7 @@ defmodule Scenic.Graph.Compiler do
defp do_text_color(ops, %{reqs: %{theme: theme}} = state) do
color =
theme
|> Theme.normalize()
|> Themes.normalize()
|> Map.get(:text)
|> Color.to_rgba()

Expand Down
Loading