I have a set of colours and would like to break them down into 10 to 20 base colours.
This should not be a pallet of what is passed in, rather independent of what is passed in. So if an image was used that was just various shades of red, it would return only red, possibly with light / dark red.
EG: the colours I have are the boxes below, with an example of the output as lines. So from 21 colours the list is down to 8 in this example.
The hex values for above:
#000000
#ffffff
#003e9f
#d61517
#00a7bd
#001070
#a0210c
#dc9103
#e6151e
#fdfdfd
#011171
#fbfd10
#ffc500
#fdc605
#e6141d
#faf703
#544b20
#796a3a
#7a6b3a
Final output could be something like the outer ring of this colour wheel
Look for a foundation to match your skin tone and undertone. Once you know your skin tone you can choose a few base colours to try. You don't want to look like you're wearing a mask so your foundation needs to blend into your neck. Test out a colour by applying some to your jawline on the side of your face.
By using a hexadecimal, or 16-bit number system, the maximum number of colors that can be displayed at any one time is 16 x 16, or 256 colors. These 256 colors are represented as the values 0 – 255. In order to convert an RGB color code to a hex color code, you will need to convert each of the values individually.
The alpha value in the hexadecimal format can be a little confusing because it's hard to conceptualize what a base 16 value for transparency will actually look like. However, the benefit is if you're already using hex codes in your codebase, now you can update them to change the transparency, too!
Pick your base
colors in advance and make an array - you could use the 12 in the outer ring of the color wheel. Use an associative array as a set for the output: for each input color, find the closest base color and add it as a key to the output array. Then return the keys of that array.
To find color distance, you can just treat the colors as points in three-dimensional space, (x,y,z) = (r,g,b), and use the Euclidian distance. After extracting the components from the hex string, that's just something like this:
$dr = $r2 - $r1;
$dg = $g2 - $g1;
$db = $b2 - $b1;
$dist = sqrt($dr * $dr + $dg * $dg + $db * $db);
You could do something fancy with octrees if you want, but with a small list of colors just looping over them and finding the smallest distance will work fine.
I think this would be a lot easier to do (and to understand) if you used hsla notation.
If your base colors are always the same, you could define 20 values for your hue parameter (which is basically an angle from 0° to 360°), and group all the different colors you have to the closest 'base color' only with the hue.
If your base colors are dependant on the input (it is not clear in your question), then @Mark Reed's option to find color distance can be done in hsl as well, only with the hue, which would be more accurate to the notion of 'shades of color' IMHO.
NB : A handy tool for hsl : http://mothereffinghsl.com/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With