Skip to content

Commit 5612ec7

Browse files
Merge pull request #1 from SirGoodenough/dev
Add the remainder of the To Do Items
2 parents acb3d15 + 429e248 commit 5612ec7

File tree

3 files changed

+722
-401
lines changed

3 files changed

+722
-401
lines changed

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
patreon: WhatAreWeFixingToday
2+
custom: ["https://www.paypal.com/paypalme/SirGoodenough", "https://www.buymeacoffee.com/SirGoodenough"]

README.md

Lines changed: 186 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Custom Template for doing things with colors.
22

3-
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-41BDF5.svg)](https://github.com/custom-components/hacs)
3+
[![hacs_badge](https://img.shields.io/badge/HACS-Default-41BDF5.svg)](https://github.com/custom-components/hacs)
44
![Version](https://img.shields.io/github/v/release/SirGoodenough/Color-Multi-Tool)
55

66
<a href="https://www.buymeacoffee.com/SirGoodenough"><img src="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.
1010

1111
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.
1212

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.
1414

1515
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.
1616

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.
1818

1919
Documentation for this Custom Template can be found at:
2020

@@ -27,10 +27,10 @@ This requires HomeAssistant version 2023.11.0 or greater due to the use of the l
2727
# 🦠 TO-DO List
2828

2929
- ~~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```~~
3434
- Push to Hacs released.
3535

3636
# 🔩 Installation
@@ -46,9 +46,9 @@ You *may* need to enable 'experimental features' mode. To do this find the HACS
4646
#### Other help
4747

4848
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.
5050

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.
5252

5353
[![Open your Home Assistant instance and show your template developer tools.](https://my.home-assistant.io/badges/developer_template.svg)](https://my.home-assistant.io/redirect/developer_template/)
5454

@@ -58,12 +58,12 @@ Another good thing to do before you ask for help is try testing what you have in
5858

5959
## `random_name()`
6060

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+
6363
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+
6565
This lets "dark seagreen" and "dark sea green" both match the same color "darkseagreen".
66-
66+
6767
### SAMPLE USAGE:
6868

6969
```jinja
@@ -78,7 +78,7 @@ Another good thing to do before you ask for help is try testing what you have in
7878
## `random_xy()`
7979

8080
This will return a CSV string that will convert to a list for a random color in the x,y format.
81-
81+
8282
SAMPLE USAGE:
8383

8484
```jinja
@@ -120,7 +120,7 @@ Another good thing to do before you ask for help is try testing what you have in
120120
## `random_rgb()`
121121

122122
This will return a CSV string that will convert to a list for a random color in the rgb format.
123-
123+
124124
SAMPLE USAGE:
125125

126126
```jinja
@@ -140,20 +140,20 @@ Another good thing to do before you ask for help is try testing what you have in
140140

141141
## `name2rgb(color_name)`
142142

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+
145145
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+
147147
This lets "dark seagreen" and "dark sea green" both match the same color "darkseagreen".
148-
148+
149149
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+
151151
This lets "dark seagreen" and "dark sea green" both match the same color "darkseagreen".
152-
152+
153153
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+
155155
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+
157157
SAMPLE USAGE:
158158

159159
```jinja
@@ -168,18 +168,38 @@ Another good thing to do before you ask for help is try testing what you have in
168168

169169
*********************
170170

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+
171191
# Return the xy Number for a Provided rgb Number
172192

173193
## `rgb2xy(rgb_formatted_list)`
174194

175195
This will return a CSV string that will convert to a list in the x,y format from the rgb list provided.
176-
196+
177197
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+
179199
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+
181201
Brightness is assumed to be 100%
182-
202+
183203
SAMPLE USAGE:
184204

185205
```jinja
@@ -200,13 +220,13 @@ Another good thing to do before you ask for help is try testing what you have in
200220
## `xy2rgb(xy_formatted_list)`
201221

202222
This will return a CSV string that will convert to a list in the rgb format from the xy list provided.
203-
223+
204224
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+
206226
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+
208228
Brightness is assumed to be 100%
209-
229+
210230
SAMPLE USAGE:
211231

212232
```jinja
@@ -227,11 +247,11 @@ Another good thing to do before you ask for help is try testing what you have in
227247
## `hs2rgb(hs_formatted_list)`
228248

229249
This will return a CSV string that will convert to a list in the rgb format from the xy list provided.
230-
250+
231251
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+
233253
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+
235255
Brightness is assumed to be 100%
236256

237257
SAMPLE USAGE:
@@ -254,15 +274,15 @@ Another good thing to do before you ask for help is try testing what you have in
254274
## `rgb2hs(rgb_formatted_list)`
255275

256276
This will return a CSV string that will convert to a list in the x,y format from the rgb list provided.
257-
277+
258278
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+
260280
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.
261281

262282
Brightness is assumed to be 100%
263-
283+
264284
SAMPLE USAGE:
265-
285+
266286
```jinja
267287
{% from 'color_multi_tool.jinja' import rgb2hs %}
268288
{% 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
281301
## `hs2xy(hs_formatted_list)`
282302

283303
This will return a CSV string that will convert to a list in the hs format from the xy list provided.
284-
304+
285305
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+
287307
This converter uses other templates provided in this Custom Jinja Template.
288-
308+
289309
Brightness is assumed to be 100%
290310

291311
SAMPLE USAGE:
@@ -308,9 +328,9 @@ Another good thing to do before you ask for help is try testing what you have in
308328
## `xy2hs(xy_formatted_list)`
309329

310330
This will return a CSV string that will convert to a list in the hs format from the xy list provided.
311-
331+
312332
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+
314334
This converter uses other templates provided in this Custom Jinja Template.
315335

316336
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
327347
Everything returned from a macro template is a string, so for
328348
conventional usage of colors the result needs to be converted to a
329349
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
461+
https://github.com/home-assistant/core/blob/dev/homeassistant/util/color.py
462+
and the Official CSS3 colors from w3.org:
463+
https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4
464+
Names do not have spaces in them so that we can compare against
465+
requests more easily (by removing spaces from the requests as well).
466+
This lets "dark seagreen" and "dark sea green" both match the same
467+
color "darkseagreen".
468+
You also get the RGB color code for each of the color names.
469+
470+
SAMPLE USAGE:
471+
{% from 'color_multi_tool.jinja' import color_list() %}
472+
{{- color_list().split('\n') | list -}}
330473

331474
### Other Info
332475

0 commit comments

Comments
 (0)