@@ -5,6 +5,26 @@ defmodule ComponentsGuideWeb.ColorLive do
5
5
defmodule State do
6
6
defstruct color: { :lab , 50 , 100 , - 128 }
7
7
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
+
8
28
def set_color ( state = % __MODULE__ { } , color ) , do: % { state | color: color }
9
29
10
30
def l ( % __MODULE__ { color: { :lab , l , _ , _ } } ) , do: l
@@ -13,8 +33,6 @@ defmodule ComponentsGuideWeb.ColorLive do
13
33
14
34
def css ( % __MODULE__ { color: color } ) , do: StylingHelpers . to_css ( color )
15
35
16
- defp clamp_0_1 ( n ) , do: n |> max ( 0 ) |> min ( 1 )
17
-
18
36
defp to_srgb ( % __MODULE__ { color: color } ) do
19
37
{ :srgb , r , g , b } = color |> StylingHelpers . convert ( :srgb ) |> StylingHelpers . clamp ( )
20
38
{ r , g , b }
@@ -23,11 +41,12 @@ defmodule ComponentsGuideWeb.ColorLive do
23
41
def hex ( state = % __MODULE__ { } ) do
24
42
{ r , g , b } = state |> to_srgb ( )
25
43
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 ( )
31
50
32
51
"##{ digits } "
33
52
end
@@ -37,11 +56,12 @@ defmodule ComponentsGuideWeb.ColorLive do
37
56
swatch_size = 100
38
57
{ :lab , l , a , b } = assigns . state . color
39
58
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
+ ] )
45
65
46
66
~L"""
47
67
<article class="text-2xl max-w-lg mx-auto text-white">
@@ -70,7 +90,7 @@ defmodule ComponentsGuideWeb.ColorLive do
70
90
<dt class="font-bold">Hex:
71
91
<dd><%= State.hex(@state) %>
72
92
<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 >
74
94
<dt class="font-bold">Gradient CSS:
75
95
<dd><pre class="text-base whitespace-pre-wrap"><code><%= gradient %></code></pre>
76
96
</dl>
@@ -82,10 +102,27 @@ defmodule ComponentsGuideWeb.ColorLive do
82
102
{ :ok , assign ( socket , state: % State { } ) }
83
103
end
84
104
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
+
85
115
def handle_event ( "lab_changed" , % { "l" => l , "a" => a , "b" => b } , socket ) do
86
116
l = l |> String . to_integer ( )
87
117
a = a |> String . to_integer ( )
88
118
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 ) }
90
127
end
91
128
end
0 commit comments