@@ -5,6 +5,26 @@ defmodule ComponentsGuideWeb.ColorLive do
55 defmodule State do
66 defstruct color: { :lab , 50 , 100 , - 128 }
77
8+ def from_input (
9+ "#" <>
10+ << r1 :: utf8 >> <>
11+ << r2 :: utf8 >> <> << g1 :: utf8 >> <> << g2 :: utf8 >> <> << b1 :: utf8 >> <> << b2 :: utf8 >>
12+ ) do
13+ # <<cp::utf8>>
14+ r = << r1 :: utf8 >> <> << r2 :: utf8 >>
15+ g = << g1 :: utf8 >> <> << g2 :: utf8 >>
16+ b = << b1 :: utf8 >> <> << b2 :: utf8 >>
17+
18+ r = String . to_integer ( r , 16 ) / 255
19+ g = String . to_integer ( g , 16 ) / 255
20+ b = String . to_integer ( b , 16 ) / 255
21+
22+ color = { :srgb , r , g , b } |> Styling . convert ( :lab )
23+
24+ IO . puts ( "INPUT #{ r } #{ g } #{ b } OUTPUT #{ inspect ( color ) } " )
25+ % State { color: color }
26+ end
27+
828 def set_color ( state = % __MODULE__ { } , color ) , do: % { state | color: color }
929
1030 def l ( % __MODULE__ { color: { :lab , l , _ , _ } } ) , do: l
@@ -13,8 +33,6 @@ defmodule ComponentsGuideWeb.ColorLive do
1333
1434 def css ( % __MODULE__ { color: color } ) , do: StylingHelpers . to_css ( color )
1535
16- defp clamp_0_1 ( n ) , do: n |> max ( 0 ) |> min ( 1 )
17-
1836 defp to_srgb ( % __MODULE__ { color: color } ) do
1937 { :srgb , r , g , b } = color |> StylingHelpers . convert ( :srgb ) |> StylingHelpers . clamp ( )
2038 { r , g , b }
@@ -23,11 +41,12 @@ defmodule ComponentsGuideWeb.ColorLive do
2341 def hex ( state = % __MODULE__ { } ) do
2442 { r , g , b } = state |> to_srgb ( )
2543
26- digits = [ r , g , b ]
27- |> Enum . map ( fn c ->
28- round ( c * 255 ) |> Integer . to_string ( 16 ) |> String . pad_leading ( 2 , "0" )
29- end )
30- |> Enum . join ( )
44+ digits =
45+ [ r , g , b ]
46+ |> Enum . map ( fn c ->
47+ round ( c * 255 ) |> Integer . to_string ( 16 ) |> String . pad_leading ( 2 , "0" )
48+ end )
49+ |> Enum . join ( )
3150
3251 "##{ digits } "
3352 end
@@ -37,11 +56,12 @@ defmodule ComponentsGuideWeb.ColorLive do
3756 swatch_size = 100
3857 { :lab , l , a , b } = assigns . state . color
3958
40- gradient = Styling . linear_gradient ( "150grad" , [
41- { :lab , l * 1.5 , a * 1.2 , b * 0.8 } ,
42- { :lab , l , a , b } ,
43- { :lab , l * 0.5 , a * 0.8 , b * 1.2 } ,
44- ] )
59+ gradient =
60+ Styling . linear_gradient ( "150grad" , [
61+ { :lab , l * 1.5 , a * 1.2 , b * 0.8 } ,
62+ { :lab , l , a , b } ,
63+ { :lab , l * 0.5 , a * 0.8 , b * 1.2 }
64+ ] )
4565
4666 ~L"""
4767 <article class="text-2xl max-w-lg mx-auto text-white">
@@ -70,7 +90,7 @@ defmodule ComponentsGuideWeb.ColorLive do
7090 <dt class="font-bold">Hex:
7191 <dd><%= State.hex(@state) %>
7292 <dt class="font-bold">CSS:
73- <dd><%= State.css(@state) %>
93+ <dd><pre class="text-base whitespace-pre-wrap"><code>< %= State.css(@state) %></code></pre >
7494 <dt class="font-bold">Gradient CSS:
7595 <dd><pre class="text-base whitespace-pre-wrap"><code><%= gradient %></code></pre>
7696 </dl>
@@ -82,10 +102,27 @@ defmodule ComponentsGuideWeb.ColorLive do
82102 { :ok , assign ( socket , state: % State { } ) }
83103 end
84104
105+ def handle_params ( % { "definition" => definition } , _path , socket ) do
106+ state = State . from_input ( definition )
107+ { :noreply , socket |> assign ( :state , state ) }
108+ end
109+
110+ def handle_params ( % { } , _path , socket ) do
111+ state = % State { }
112+ { :noreply , socket |> assign ( :state , state ) }
113+ end
114+
85115 def handle_event ( "lab_changed" , % { "l" => l , "a" => a , "b" => b } , socket ) do
86116 l = l |> String . to_integer ( )
87117 a = a |> String . to_integer ( )
88118 b = b |> String . to_integer ( )
89- { :noreply , assign ( socket , :state , socket . assigns . state |> State . set_color ( { :lab , l , a , b } ) ) }
119+
120+ state = socket . assigns . state |> State . set_color ( { :lab , l , a , b } )
121+ hex = state |> State . hex ( )
122+
123+ { :noreply ,
124+ socket
125+ |> assign ( :state , state )
126+ |> push_patch ( to: Routes . color_path ( socket , :show , hex ) , replace: true ) }
90127 end
91128end
0 commit comments