You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<ahref="https://www.buymeacoffee.com/SirGoodenough"><imgsrc="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=SirGoodenough&button_colour=5F7FFF&font_colour=ffffff&font_family=Poppins&outline_colour=000000&coffee_colour=FFDD00"width=auto,height=30/></a>
@@ -10,11 +10,11 @@ Custom Template for doing things with colors.
10
10
11
11
Custom Template for doing things with colors. It started out by wanting a way to get a random color name off of the official color name list. Then I decided it needed a way to pull random rgb, hs, and xy colors as well. I then decided to build the conversions between all the types.
12
12
13
-
The conversions are derived from HA's own color conversion code with the exception of rgb2hs. Home Assistant uses the built-in python module to do that in core, so I pulled the code for that from another source.
13
+
The conversions are derived from HA's own color conversion code with the exception of rgb2hs. Home Assistant uses the built-in python module to do that in core, so I pulled the code for that from another source.
14
14
15
15
I followed the conversion code as close as I could, but I find that if you convert a color then convert it back, it drifts quite a bit.
16
16
17
-
I welcome the PR from anyone that can improve any of this code. It is 'functional' but not optimized at this point, and there are things I want to do with it. However I wanted to release it for others to collab at this point.
17
+
I welcome the PR from anyone that can improve any of this code. It is 'functional' but not optimized at this point. In several cases I don't understand the code, I just ported it over, hopefully successfully. PR's are welcome.
18
18
19
19
Documentation for this Custom Template can be found at:
20
20
@@ -27,10 +27,10 @@ This requires HomeAssistant version 2023.11.0 or greater due to the use of the l
27
27
# 🦠 TO-DO List
28
28
29
29
-~~Push to HACS Custom.~~
30
-
- Error checking inputs for range.
31
-
-Add ```Display closest color name to a given RGB```.
32
-
- Clean-up code, add shortcuts on redundancies.
33
-
-Return a list of all the official color names.
30
+
-~~Error checking inputs for range.~~
31
+
-~~Return a list of all the official color names.~~
32
+
-~~Clean-up code, add shortcuts on redundancies.~~
33
+
-~~Add ```Display closest color name to a given RGB```~~
34
34
- Push to Hacs released.
35
35
36
36
# 🔩 Installation
@@ -46,9 +46,9 @@ You *may* need to enable 'experimental features' mode. To do this find the HACS
46
46
#### Other help
47
47
48
48
Before you report a 'Bug', you need to make sure you have read the accompanying Descriptions on the templates and this README and have followed all the settings required here.
49
-
This is important because if these instructions are not followed, you will likely have a bad day and be forced to contact me for help. Not that I don't want to help, but personal interaction takes me a while to respond and is generally non-productive.
49
+
This is important because if these instructions are not followed, you will likely have a bad day and be forced to contact me for help. Not that I don't want to help, but personal interaction takes me a while to respond and is generally non-productive.
50
50
51
-
Another good thing to do before you ask for help is try testing what you have in the Developer Tools Template Tab in Home Assistant. There you can adjust this and that until you figure out the answer to your question yourself.
51
+
Another good thing to do before you ask for help is try testing what you have in the Developer Tools Template Tab in Home Assistant. There you can adjust this and that until you figure out the answer to your question yourself. At the end of the color table in the code is the developer code that I use to test everything. If you provide any PR's make sure you load this and test all the things.
52
52
53
53
[](https://my.home-assistant.io/redirect/developer_template/)
54
54
@@ -58,12 +58,12 @@ Another good thing to do before you ask for help is try testing what you have in
58
58
59
59
## `random_name()`
60
60
61
-
This will return one of the official color names recognized by Home Assistant lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py and the Official CSS3 colors from w3.org: https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4
62
-
61
+
This will return one of the official color names recognized by Home Assistant lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py and the Official CSS3 colors from w3.org: https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4
62
+
63
63
Names do not have spaces in them so that we can compare against requests more easily (by removing spaces from the requests as well).
64
-
64
+
65
65
This lets "dark seagreen" and "dark sea green" both match the same color "darkseagreen".
66
-
66
+
67
67
### SAMPLE USAGE:
68
68
69
69
```jinja
@@ -78,7 +78,7 @@ Another good thing to do before you ask for help is try testing what you have in
78
78
## `random_xy()`
79
79
80
80
This will return a CSV string that will convert to a list for a random color in the x,y format.
81
-
81
+
82
82
SAMPLE USAGE:
83
83
84
84
```jinja
@@ -120,7 +120,7 @@ Another good thing to do before you ask for help is try testing what you have in
120
120
## `random_rgb()`
121
121
122
122
This will return a CSV string that will convert to a list for a random color in the rgb format.
123
-
123
+
124
124
SAMPLE USAGE:
125
125
126
126
```jinja
@@ -140,20 +140,20 @@ Another good thing to do before you ask for help is try testing what you have in
140
140
141
141
## `name2rgb(color_name)`
142
142
143
-
This will return the RGB value for an official color name recognized by Home Assistant lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py and the Official CSS3 colors from w3.org: https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4
144
-
143
+
This will return the RGB value for an official color name recognized by Home Assistant lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py and the Official CSS3 colors from w3.org: https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4
144
+
145
145
Names do not have spaces in them so that we can compare against requests more easily (by removing spaces from the requests as well).
146
-
146
+
147
147
This lets "dark seagreen" and "dark sea green" both match the same color "darkseagreen".
148
-
148
+
149
149
Names do not have spaces in them so that we can compare against requests more easily (by removing spaces from the requests as well)
150
-
150
+
151
151
This lets "dark seagreen" and "dark sea green" both match the same color "darkseagreen".
152
-
152
+
153
153
You have to type in her correct name. The text you send me will be set to no spaces and lower case to match the data as described above.
154
-
154
+
155
155
If the name is not found, it will return "0.0.0", IE black, because then name you provided is not a color.
156
-
156
+
157
157
SAMPLE USAGE:
158
158
159
159
```jinja
@@ -168,18 +168,38 @@ Another good thing to do before you ask for help is try testing what you have in
168
168
169
169
*********************
170
170
171
+
# Return the Color Name for a Provided rgb Number
172
+
173
+
## `rgb2name(_range, rgbl)`
174
+
175
+
This template will accept a list representing an RGB value plus a range value representing the window size of the 'close enough' color name. It will return the Home Assistant Color name that matches it or is close to it within a +/- range you specify.
176
+
177
+
- If you want only an exact match, then set the range to 0.
178
+
- If you think that +10/-10 is close enough, then set the range to 10.
179
+
180
+
For default the color is set to black [0,0,0] and the range is 0, so it will by give you ['black'] for invalid input.
181
+
182
+
SAMPLE USAGE:
183
+
{% from 'color_multi_tool.jinja' import rgb2name %}
184
+
{{ name2rgb(color_name) }}
185
+
186
+
REMEMBER:
187
+
Everything returned from a macro template is a string.
188
+
189
+
*********************
190
+
171
191
# Return the xy Number for a Provided rgb Number
172
192
173
193
## `rgb2xy(rgb_formatted_list)`
174
194
175
195
This will return a CSV string that will convert to a list in the x,y format from the rgb list provided.
176
-
196
+
177
197
Be sure to convert this to a list when you use it on the other end because it arrives as a CSV string.
178
-
198
+
179
199
Code lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py.
180
-
200
+
181
201
Brightness is assumed to be 100%
182
-
202
+
183
203
SAMPLE USAGE:
184
204
185
205
```jinja
@@ -200,13 +220,13 @@ Another good thing to do before you ask for help is try testing what you have in
200
220
## `xy2rgb(xy_formatted_list)`
201
221
202
222
This will return a CSV string that will convert to a list in the rgb format from the xy list provided.
203
-
223
+
204
224
Be sure to convert this to a list when you use it on the other end because it arrives as a CSV string.
205
-
225
+
206
226
Code lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py.
207
-
227
+
208
228
Brightness is assumed to be 100%
209
-
229
+
210
230
SAMPLE USAGE:
211
231
212
232
```jinja
@@ -227,11 +247,11 @@ Another good thing to do before you ask for help is try testing what you have in
227
247
## `hs2rgb(hs_formatted_list)`
228
248
229
249
This will return a CSV string that will convert to a list in the rgb format from the xy list provided.
230
-
250
+
231
251
Be sure to convert this to a list when you use it on the other end because it arrives as a CSV string.
232
-
252
+
233
253
Code lifted directly from the Home Assistant core github November, 2023 at https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py.
234
-
254
+
235
255
Brightness is assumed to be 100%
236
256
237
257
SAMPLE USAGE:
@@ -254,15 +274,15 @@ Another good thing to do before you ask for help is try testing what you have in
254
274
## `rgb2hs(rgb_formatted_list)`
255
275
256
276
This will return a CSV string that will convert to a list in the x,y format from the rgb list provided.
257
-
277
+
258
278
Be sure to convert this to a list when you use it on the other end because it arrives as a CSV string.
259
-
279
+
260
280
Code lifted directly from the here November, 2023 at https://www.cs.rit.edu/~ncs/color/t_convert.html#RGB%20to%20HSV%20&%20HSV%20to%20RGB.
261
281
262
282
Brightness is assumed to be 100%
263
-
283
+
264
284
SAMPLE USAGE:
265
-
285
+
266
286
```jinja
267
287
{% from 'color_multi_tool.jinja' import rgb2hs %}
268
288
{% set _rgb2hs = rgb2hs(rgbl).split(",") | list -%}
@@ -281,11 +301,11 @@ Another good thing to do before you ask for help is try testing what you have in
281
301
## `hs2xy(hs_formatted_list)`
282
302
283
303
This will return a CSV string that will convert to a list in the hs format from the xy list provided.
284
-
304
+
285
305
Be sure to convert this to a list when you use it on the other end because it arrives as a CSV string.
286
-
306
+
287
307
This converter uses other templates provided in this Custom Jinja Template.
288
-
308
+
289
309
Brightness is assumed to be 100%
290
310
291
311
SAMPLE USAGE:
@@ -308,9 +328,9 @@ Another good thing to do before you ask for help is try testing what you have in
308
328
## `xy2hs(xy_formatted_list)`
309
329
310
330
This will return a CSV string that will convert to a list in the hs format from the xy list provided.
311
-
331
+
312
332
Be sure to convert this to a list when you use it on the other end because it arrives as a CSV string.
313
-
333
+
314
334
This converter uses other templates provided in this Custom Jinja Template.
315
335
316
336
Brightness is assumed to be 100%
@@ -327,6 +347,129 @@ Another good thing to do before you ask for help is try testing what you have in
327
347
Everything returned from a macro template is a string, so for
328
348
conventional usage of colors the result needs to be converted to a
329
349
list as shown in the example above.
350
+
list as shown in the example above.
351
+
352
+
*********************
353
+
354
+
# Test if a list is a valid RGB color list
355
+
356
+
## `chkRGB(_rgbl)`
357
+
358
+
This will test a list to make sure it’s a valid RGB color list.
359
+
360
+
First we need to make sure it’s a list, and that the list has 3 members.
361
+
Then we need to test that each member is a number, an integer, and between 0 & 255.
362
+
If all that is true return True.
363
+
If any is not true, return False.
364
+
Development credit to @Didgeridrew and @jeffcrum on the
365
+
[Home Assistant Community Forums](https://community.home-assistant.io/t/help-with-test-of-a-list-of-numbers/664839/3)
366
+
for help sorting this out.
367
+
368
+
SAMPLE USAGE:
369
+
370
+
```jinja
371
+
{% from 'color_multi_tool.jinja' import chkRGB %}
372
+
{{ chkRGB([255,165,0]) | bool }}
373
+
```
374
+
375
+
REMEMBER:
376
+
This always returns text, so cast to bool on the other end to be
377
+
certain of the result.
378
+
379
+
*********************
380
+
381
+
# Test if a list is a valid XY color list
382
+
383
+
## `chkXY(_xyl)`
384
+
385
+
This will test a list to make sure it’s a valid XY color list.
386
+
387
+
First we need to make sure it’s a list, and that the list has 2 members.
388
+
Then we need to test that each member is a number, and between 0 & 1.
389
+
If all that is true return True.
390
+
If any is not true, return False.
391
+
Development credit to @123 on the
392
+
[Home Assistant Community Forums](https://community.home-assistant.io/t/help-with-test-of-a-list-of-numbers/664839/5)
393
+
for help sorting this out.
394
+
395
+
SAMPLE USAGE:
396
+
397
+
```jinja
398
+
{% from 'color_multi_tool.jinja' import chkXY %}
399
+
{{ chkXY([0.497,0.472]) | bool }}
400
+
```
401
+
402
+
REMEMBER:
403
+
This always returns text, so cast to bool on the other end to be
404
+
certain of the result.
405
+
406
+
*********************
407
+
408
+
# Test if a list is a valid HS color list
409
+
410
+
## `chkHS(_hsl)`
411
+
412
+
This will test a list to make sure it’s a valid HS color list.
413
+
414
+
First we need to make sure it’s a list, and that the list has 2 members.
415
+
Then we need to test that each member is a number.
416
+
The first one has to be 0 to 100, the second one has to be 0 to 360.
417
+
If all that is true return True.
418
+
If any is not true, return False.
419
+
Development credit to @123 on the
420
+
[Home Assistant Community Forums](https://community.home-assistant.io/t/help-with-test-of-a-list-of-numbers/664839/5)
421
+
for help sorting this out.
422
+
423
+
SAMPLE USAGE:
424
+
425
+
```jinja
426
+
{% from 'color_multi_tool.jinja' import chkHS %}
427
+
{{ chkHS([38.824,100.0]) | bool }}
428
+
```
429
+
430
+
REMEMBER:
431
+
This always returns text, so cast to bool on the other end to be
432
+
certain of the result.
433
+
434
+
*********************
435
+
436
+
## `chkNAME(color_name)`
437
+
438
+
Is this color name on the official list?
439
+
This will test a provided color name to see if it is on the HA color list.
440
+
441
+
First we need to make sure it’s a string_like variable.
442
+
Then we test that the string is only lowercase letters to match the HA list.
443
+
Then we iterate thru the list to make sure it is on the list.
444
+
If all that is true return true.
445
+
If any is not true, return false.
446
+
447
+
SAMPLE USAGE:
448
+
{% from 'color_multi_tool.jinja' import chkNAME %}
449
+
{{ chkNAME('orange') | bool }}
450
+
451
+
REMEMBER:
452
+
This always returns text, so cast to bool on the other end to be
453
+
certain of the result.
454
+
455
+
*********************
456
+
457
+
## `color_list()`
458
+
459
+
This will return all of the official color names recognized by Home Assistant
460
+
lifted directly from the Home Assistant core github November, 2023 at
0 commit comments