Skip to content

Instantly share code, notes, and snippets.

@tomkail
Last active September 14, 2022 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkail/7832d8a3e5e69d6c74bd66e7e0e900d5 to your computer and use it in GitHub Desktop.
Save tomkail/7832d8a3e5e69d6c74bd66e7e0e900d5 to your computer and use it in GitHub Desktop.
Describes a color in english. Crude implementation, but handy for scanning colors when shown in text fields.
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public static class ColorDescriptor {
public static string GetNameForColor (Color color) {
var bestDist = float.MaxValue;
string best = null;
foreach(var colorName in colorNames) {
var dist = GetColorDistance(color, colorName.Value);
if(dist < bestDist) {
best = colorName.Key;
bestDist = dist;
}
}
return best;
}
// This is a terrible but basic implementation. Refer to https://stackoverflow.com/questions/9018016/how-to-compare-two-colors-for-similarity-difference
public static float GetColorDistance (Color c1, Color c2) {
float diffRed = Mathf.Abs(c1.r - c2.r);
float diffGreen = Mathf.Abs(c1.g - c2.g);
float diffBlue = Mathf.Abs(c1.b - c2.b);
float pctDiffRed = diffRed / 255;
float pctDiffGreen = diffGreen / 255;
float pctDiffBlue = diffBlue / 255;
return (pctDiffRed + pctDiffGreen + pctDiffBlue) / 3f * 100;
}
// From this list: https://docs.microsoft.com/en-us/power-platform/power-fx/reference/function-colors
static List<KeyValuePair<string, Color>> colorNames = new List<KeyValuePair<string, Color>>() {
new KeyValuePair<string, Color>("Alice Blue", new Color(240, 248, 255, 1)),
new KeyValuePair<string, Color>("Antique White", new Color(250, 235, 215, 1)),
new KeyValuePair<string, Color>("Aqua", new Color(0, 255, 255, 1)),
new KeyValuePair<string, Color>("Aquamarine", new Color(127, 255, 212, 1)),
new KeyValuePair<string, Color>("Azure", new Color(240, 255, 255, 1)),
new KeyValuePair<string, Color>("Beige", new Color(245, 245, 220, 1)),
new KeyValuePair<string, Color>("Bisque", new Color(255, 228, 196, 1)),
new KeyValuePair<string, Color>("Black", new Color(0, 0, 0, 1)),
new KeyValuePair<string, Color>("Blanched Almond", new Color(255, 235, 205, 1)),
new KeyValuePair<string, Color>("Blue", new Color(0, 0, 255, 1)),
new KeyValuePair<string, Color>("Blue Violet", new Color(138, 43, 226, 1)),
new KeyValuePair<string, Color>("Brown", new Color(165, 42, 42, 1)),
new KeyValuePair<string, Color>("Burlywood", new Color(222, 184, 135, 1)),
new KeyValuePair<string, Color>("Cadet Blue", new Color(95, 158, 160, 1)),
new KeyValuePair<string, Color>("Chartreuse", new Color(127, 255, 0, 1)),
new KeyValuePair<string, Color>("Chocolate", new Color(210, 105, 30, 1)),
new KeyValuePair<string, Color>("Coral", new Color(255, 127, 80, 1)),
new KeyValuePair<string, Color>("Cornflower Blue", new Color(100, 149, 237, 1)),
new KeyValuePair<string, Color>("Cornsilk", new Color(255, 248, 220, 1)),
new KeyValuePair<string, Color>("Crimson", new Color(220, 20, 60, 1)),
new KeyValuePair<string, Color>("Cyan", new Color(0, 255, 255, 1)),
new KeyValuePair<string, Color>("Dark Blue", new Color(0, 0, 139, 1)),
new KeyValuePair<string, Color>("Dark Cyan", new Color(0, 139, 139, 1)),
new KeyValuePair<string, Color>("Dark GoldenRod", new Color(184, 134, 11, 1)),
new KeyValuePair<string, Color>("Dark Gray", new Color(169, 169, 169, 1)),
new KeyValuePair<string, Color>("Dark Green", new Color(0, 100, 0, 1)),
new KeyValuePair<string, Color>("Dark Khaki", new Color(189, 183, 107, 1)),
new KeyValuePair<string, Color>("Dark Magenta", new Color(139, 0, 139, 1)),
new KeyValuePair<string, Color>("Dark Olive Green", new Color(85, 107, 47, 1)),
new KeyValuePair<string, Color>("Dark Orange", new Color(255, 140, 0, 1)),
new KeyValuePair<string, Color>("Dark Orchid", new Color(153, 50, 204, 1)),
new KeyValuePair<string, Color>("Dark Red", new Color(139, 0, 0, 1)),
new KeyValuePair<string, Color>("Dark Salmon", new Color(233, 150, 122, 1)),
new KeyValuePair<string, Color>("Dark Sea Green", new Color(143, 188, 143, 1)),
new KeyValuePair<string, Color>("Dark Slate Blue", new Color(72, 61, 139, 1)),
new KeyValuePair<string, Color>("Dark Slate Gray", new Color(47, 79, 79, 1)),
new KeyValuePair<string, Color>("Dark Turquoise", new Color(0, 206, 209, 1)),
new KeyValuePair<string, Color>("Dark Violet", new Color(148, 0, 211, 1)),
new KeyValuePair<string, Color>("Deep Pink", new Color(255, 20, 147, 1)),
new KeyValuePair<string, Color>("Deep Sky Blue", new Color(0, 191, 255, 1)),
new KeyValuePair<string, Color>("Dim Gray", new Color(105, 105, 105, 1)),
new KeyValuePair<string, Color>("Dodger Blue", new Color(30, 144, 255, 1)),
new KeyValuePair<string, Color>("FireBrick", new Color(178, 34, 34, 1)),
new KeyValuePair<string, Color>("Floral White", new Color(255, 250, 240, 1)),
new KeyValuePair<string, Color>("Forest Green", new Color(34, 139, 34, 1)),
new KeyValuePair<string, Color>("Fuchsia", new Color(255, 0, 255, 1)),
new KeyValuePair<string, Color>("Gainsboro", new Color(220, 220, 220, 1)),
new KeyValuePair<string, Color>("Ghost White", new Color(248, 248, 255, 1)),
new KeyValuePair<string, Color>("Gold", new Color(255, 215, 0, 1)),
new KeyValuePair<string, Color>("GoldenRod", new Color(218, 165, 32, 1)),
new KeyValuePair<string, Color>("Gray", new Color(128, 128, 128, 1)),
new KeyValuePair<string, Color>("Green", new Color(0, 128, 0, 1)),
new KeyValuePair<string, Color>("Green Yellow", new Color(173, 255, 47, 1)),
new KeyValuePair<string, Color>("Honeydew", new Color(240, 255, 240, 1)),
new KeyValuePair<string, Color>("Hot Pink", new Color(255, 105, 180, 1)),
new KeyValuePair<string, Color>("Indian Red", new Color(205, 92, 92, 1)),
new KeyValuePair<string, Color>("Indigo", new Color(75, 0, 130, 1)),
new KeyValuePair<string, Color>("Ivory", new Color(255, 255, 240, 1)),
new KeyValuePair<string, Color>("Khaki", new Color(240, 230, 140, 1)),
new KeyValuePair<string, Color>("Lavender", new Color(230, 230, 250, 1)),
new KeyValuePair<string, Color>("Lavender Blush", new Color(255, 240, 245, 1)),
new KeyValuePair<string, Color>("Lawn Green", new Color(124, 252, 0, 1)),
new KeyValuePair<string, Color>("Lemon Chiffon", new Color(255, 250, 205, 1)),
new KeyValuePair<string, Color>("Light Blue", new Color(173, 216, 230, 1)),
new KeyValuePair<string, Color>("Light Coral", new Color(240, 128, 128, 1)),
new KeyValuePair<string, Color>("Light Cyan", new Color(224, 255, 255, 1)),
new KeyValuePair<string, Color>("Light GoldenRod Yellow", new Color(250, 250, 210, 1)),
new KeyValuePair<string, Color>("Light Gray", new Color(211, 211, 211, 1)),
new KeyValuePair<string, Color>("Light Green", new Color(144, 238, 144, 1)),
new KeyValuePair<string, Color>("Light Pink", new Color(255, 182, 193, 1)),
new KeyValuePair<string, Color>("Light Salmon", new Color(255, 160, 122, 1)),
new KeyValuePair<string, Color>("Light SeaGreen", new Color(32, 178, 170, 1)),
new KeyValuePair<string, Color>("Light SkyBlue", new Color(135, 206, 250, 1)),
new KeyValuePair<string, Color>("Light SlateGray", new Color(119, 136, 153, 1)),
new KeyValuePair<string, Color>("Light SteelBlue", new Color(176, 196, 222, 1)),
new KeyValuePair<string, Color>("Light Yellow", new Color(255, 255, 224, 1)),
new KeyValuePair<string, Color>("Lime", new Color(0, 255, 0, 1)),
new KeyValuePair<string, Color>("Lime Green", new Color(50, 205, 50, 1)),
new KeyValuePair<string, Color>("Linen", new Color(250, 240, 230, 1)),
new KeyValuePair<string, Color>("Magenta", new Color(255, 0, 255, 1)),
new KeyValuePair<string, Color>("Maroon", new Color(128, 0, 0, 1)),
new KeyValuePair<string, Color>("Medium Aquamarine", new Color(102, 205, 170, 1)),
new KeyValuePair<string, Color>("Medium Blue", new Color(0, 0, 205, 1)),
new KeyValuePair<string, Color>("Medium Orchid", new Color(186, 85, 211, 1)),
new KeyValuePair<string, Color>("Medium Purple", new Color(147, 112, 219, 1)),
new KeyValuePair<string, Color>("Medium SeaGreen", new Color(60, 179, 113, 1)),
new KeyValuePair<string, Color>("Medium SlateBlue", new Color(123, 104, 238, 1)),
new KeyValuePair<string, Color>("Medium SpringGreen", new Color(0, 250, 154, 1)),
new KeyValuePair<string, Color>("Medium Turquoise", new Color(72, 209, 204, 1)),
new KeyValuePair<string, Color>("Medium VioletRed", new Color(199, 21, 133, 1)),
new KeyValuePair<string, Color>("Midnight Blue", new Color(25, 25, 112, 1)),
new KeyValuePair<string, Color>("Mint Cream", new Color(245, 255, 250, 1)),
new KeyValuePair<string, Color>("Misty Rose", new Color(255, 228, 225, 1)),
new KeyValuePair<string, Color>("Moccasin", new Color(255, 228, 181, 1)),
new KeyValuePair<string, Color>("Navajo White", new Color(255, 222, 173, 1)),
new KeyValuePair<string, Color>("Navy", new Color(0, 0, 128, 1)),
new KeyValuePair<string, Color>("Old Lace", new Color(253, 245, 230, 1)),
new KeyValuePair<string, Color>("Olive", new Color(128, 128, 0, 1)),
new KeyValuePair<string, Color>("Olive Drab", new Color(107, 142, 35, 1)),
new KeyValuePair<string, Color>("Orange", new Color(255, 165, 0, 1)),
new KeyValuePair<string, Color>("Orange Red", new Color(255, 69, 0, 1)),
new KeyValuePair<string, Color>("Orchid", new Color(218, 112, 214, 1)),
new KeyValuePair<string, Color>("Pale GoldenRod", new Color(238, 232, 170, 1)),
new KeyValuePair<string, Color>("Pale Green", new Color(152, 251, 152, 1)),
new KeyValuePair<string, Color>("Pale Turquoise", new Color(175, 238, 238, 1)),
new KeyValuePair<string, Color>("Pale Violet Red", new Color(219, 112, 147, 1)),
new KeyValuePair<string, Color>("Papaya Whip", new Color(255, 239, 213, 1)),
new KeyValuePair<string, Color>("Peach Puff", new Color(255, 218, 185, 1)),
new KeyValuePair<string, Color>("Peru", new Color(205, 133, 63, 1)),
new KeyValuePair<string, Color>("Pink", new Color(255, 192, 203, 1)),
new KeyValuePair<string, Color>("Plum", new Color(221, 160, 221, 1)),
new KeyValuePair<string, Color>("Powder Blue", new Color(176, 224, 230, 1)),
new KeyValuePair<string, Color>("Purple", new Color(128, 0, 128, 1)),
new KeyValuePair<string, Color>("Red", new Color(255, 0, 0, 1)),
new KeyValuePair<string, Color>("Rosy Brown", new Color(188, 143, 143, 1)),
new KeyValuePair<string, Color>("Royal Blue", new Color(65, 105, 225, 1)),
new KeyValuePair<string, Color>("Saddle Brown", new Color(139, 69, 19, 1)),
new KeyValuePair<string, Color>("Salmon", new Color(250, 128, 114, 1)),
new KeyValuePair<string, Color>("Sandy Brown", new Color(244, 164, 96, 1)),
new KeyValuePair<string, Color>("Sea Green", new Color(46, 139, 87, 1)),
new KeyValuePair<string, Color>("Sea Shell", new Color(255, 245, 238, 1)),
new KeyValuePair<string, Color>("Sienna", new Color(160, 82, 45, 1)),
new KeyValuePair<string, Color>("Silver", new Color(192, 192, 192, 1)),
new KeyValuePair<string, Color>("Sky Blue", new Color(135, 206, 235, 1)),
new KeyValuePair<string, Color>("Slate Blue", new Color(106, 90, 205, 1)),
new KeyValuePair<string, Color>("Slate Gray", new Color(112, 128, 144, 1)),
new KeyValuePair<string, Color>("Snow", new Color(255, 250, 250, 1)),
new KeyValuePair<string, Color>("Spring Green", new Color(0, 255, 127, 1)),
new KeyValuePair<string, Color>("Steel Blue", new Color(70, 130, 180, 1)),
new KeyValuePair<string, Color>("Tan", new Color(210, 180, 140, 1)),
new KeyValuePair<string, Color>("Teal", new Color(0, 128, 128, 1)),
new KeyValuePair<string, Color>("Thistle", new Color(216, 191, 216, 1)),
new KeyValuePair<string, Color>("Tomato", new Color(255, 99, 71, 1)),
new KeyValuePair<string, Color>("Transparent", new Color(0, 0, 0, 0)),
new KeyValuePair<string, Color>("Turquoise", new Color(64, 224, 208, 1)),
new KeyValuePair<string, Color>("Violet", new Color(238, 130, 238, 1)),
new KeyValuePair<string, Color>("Wheat", new Color(245, 222, 179, 1)),
new KeyValuePair<string, Color>("White", new Color(255, 255, 255, 1)),
new KeyValuePair<string, Color>("White Smoke", new Color(245, 245, 245, 1)),
new KeyValuePair<string, Color>("Yellow", new Color(255, 255, 0, 1)),
new KeyValuePair<string, Color>("Yellow Green", new Color(154, 205, 50, 1)),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment