Mattertools - Get Expanded Color Names

This posting explains how to use the getExpandedColorNames library from my matterTools library files.

The color name tools library is found in the file "matterTools.getExpandedColorNames.groovy" at the github site below.

1. Methods

This library implements a replacement for Hubitat's convertHueToGenericColorName function . This expands on Hubitat's built-in methods to provide for a greatly expanded list of color names that are determined based on Hue, Saturation, and Brightness or on RGB. The library implements a nearest-neighbor search algorithm to find the closest defined color for a set of RGB values.

Color names are from the list here: Java Code to get a color name from rgb/hex value/awt color. The part of looking up a color name from the rgb values is edited from * https://gist.github.com/nightlark/6482130 by Ryan Mast (nightlark) ยท GitHub

Comments in the file show where color names match values specified in the Matter specifications.

1.1 getColorNameFromHSV

String getColorNameFromHSV(hue: null, saturation: null, level: null) 
     hue: Integer (following Hubitat convention, specified as a percent 0-100)
     saturation: Integer (specified as a percent 0-100)
     level: Integer (specified as a percent 0-100)

This method implements a color naming function that returns a color name based on input hue, saturation, level values. Note that these hue, saturation, level values correspond to those in a Hubitat COLOR_MAP (see ColorControl capability here: Driver Capability List | Hubitat Documentation) so you can also pass a Hubitat COLOR_MAP directly to this method.

1.2 getColorNameFromRGB

String getColorNameFromRGB(r: null, g: null, b: null)
     r: Integer
     g: Integer
     b: Integer

This method implements a color naming function that returns a name based on input r, g, b values. Can simply pass the function a Hubitat "Colormap".

2. How it works

For those interested, here's how it works . . .

  • You can think of colors as points in 3D space where the R, G, B value of color is like the X, Y, Z coordinate in 3D space.
  • In 3D space, the distance between points is determined by the formula:
distance = Sqrt ( (X1-x2) ^2 + (Y1-y2)^2 + (Z1-z2)^2 )
where        X1, Y1, Z1 are the coordinates of the first point, and
             x2, y2, z2 are the coordinates of the second point.

Applying this formula, the distance between two colors would similarly be:

distance = Sqrt ( (R1-r2) ^2 + (G1-g2)^2 + (B1-b2)^2 )

So, for any color r,g,b value set you pass to the getColorNameFromRGB, what you want to do is find the defined color in the colorNames list that has RGB values "closest" in distance to the color you have input and return its names

In Groovy, this is easy to accomplish using the "min" method with the single line of code:

return colorNames.min { (inputs.r - it.r)**2 + (inputs.g - it.g)**2 + (inputs.b - it.b)**2 }.name

This single line takes every entry in the colorNames List and computes a distance between the RGB supplied to the function (inputs.r, inputs.g, inputs.b) and a particular color name in the colorNames list (using its values it.r, it.g, it.b)). The colorNames color entry with minimum distance is then determined by the min function and that color's name is returned.

Note that, for efficiency, it is not necessary to apply the Squareroot to the distance formula since simply comparing the (R1-r2) ^2 + (G1-g2)^2 + (B1-b2)^2 portions will give the same "minimal" value as if you had taken the square root of all of these and compared that. Thus, for efficiency, the sqrt calculation was left out.

3. Location of Library

2 Likes

fantastic!
will you be generating a reference library for CT bulbs?

I wasn't planning to. I think the "standard" mehod convertTemperatureToGenericColorName covers the color temperatures and I don't have anything I'd want to add to it (see here: Common Methods | Hubitat Documentation ). There's also a standard function there for the basic color names ( convertHueToGenericColorName ), but what've I've provided has a larger variety of named colors.

1 Like