GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ColorNamesSharp/ColorNames.cs at main · vycdev/ColorNamesSharp Expand file tree/ Copy pathColorNames.csMore file actions 287 lines (250 loc) · 9.1 KB / Copy pathColorNames.csFile metadata and controls287 lines (250 loc) · 9.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 using ColorNamesSharp.Tree; using ColorNamesSharp.Utility; namespace ColorNamesSharp; /// <summary> /// Finds exact or perceptually nearest matches in a configured collection of named colors. /// </summary> public class ColorNames { private readonly NamedColor[] namedColors; private readonly Dictionary<string, NamedColor> colorsByName; private readonly Dictionary<string, NamedColor> colorsByHex; /// <summary> /// The configured colors in their original insertion order. /// </summary> public IReadOnlyList<NamedColor> Colors { get; } /// <summary>Gets the root of the CIELAB KD-tree used for nearest-neighbor searches.</summary> public KDNode? ColorTreeRoot { get; } /// <summary>Creates a lookup over the supplied colors.</summary> /// <param name="namedColors">The colors to index, in insertion order.</param> /// <exception cref="ArgumentNullException"><paramref name="namedColors"/> is <see langword="null"/>.</exception> public ColorNames(IEnumerable<NamedColor> namedColors) { if (namedColors == null) throw new ArgumentNullException(nameof(namedColors)); this.namedColors = namedColors.ToArray(); Colors = Array.AsReadOnly(this.namedColors); ColorTreeRoot = KDTreeBuilder.Build(this.namedColors.ToList(), 0); colorsByName = new Dictionary<string, NamedColor>(StringComparer.OrdinalIgnoreCase); colorsByHex = new Dictionary<string, NamedColor>(StringComparer.OrdinalIgnoreCase); foreach (NamedColor color in this.namedColors) { if (!colorsByName.ContainsKey(color.Name)) colorsByName.Add(color.Name, color); if (!colorsByHex.ContainsKey(color.Hex)) colorsByHex.Add(color.Hex, color); } } /// <summary> /// Tries to get an exact color by name using a case-insensitive lookup. /// If duplicate names are configured, the first color is returned. /// </summary> /// <param name="name">The color name.</param> /// <param name="color">The matching color, or null when no match exists.</param> /// <returns>True when a matching color exists; otherwise, false.</returns> public bool TryGetByName(string? name, out NamedColor? color) { if (name == null) { color = null; return false; } return colorsByName.TryGetValue(name, out color); } /// <summary> /// Tries to get an exact color by six-digit hex value using a case-insensitive lookup. /// If duplicate values are configured, the first color is returned. /// </summary> /// <param name="hex">The color value in #RRGGBB format.</param> /// <param name="color">The matching color, or null when no match exists.</param> /// <returns>True when a matching color exists; otherwise, false.</returns> public bool TryGetByHex(string? hex, out NamedColor? color) { if (hex == null) { color = null; return false; } return colorsByHex.TryGetValue(hex, out color); } /// <summary> /// Gets a random color from the configured color list. /// </summary> /// <returns>A random color, or <see langword="null"/> when the list is empty.</returns> public NamedColor? GetRandomNamedColor() { if (namedColors.Length == 0) return null; Random random = new(); int index = random.Next(namedColors.Length); return namedColors[index]; } /// <summary> /// Searches for the closest node to the given LAB color values. /// </summary> /// <param name="node"></param> /// <param name="l"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="depth"></param> /// <param name="minDistance"></param> /// <param name="bestNode"></param> private static void SearchNearest( KDNode? node, float l, float a, float b, int depth, ref double minDistance, ref KDNode? bestNode) { if (node == null) return; int axis = depth % 3; (float, float, float) nodeLab = node.Color.Lab; float dim = axis switch { 0 => l - nodeLab.Item1, 1 => a - nodeLab.Item2, _ => b - nodeLab.Item3 }; double dist = Math.Sqrt( (l - nodeLab.Item1) * (l - nodeLab.Item1) + (a - nodeLab.Item2) * (a - nodeLab.Item2) + (b - nodeLab.Item3) * (b - nodeLab.Item3)); if (dist < minDistance) { minDistance = dist; bestNode = node; } KDNode? firstChild = dim <= 0 ? node.Left : node.Right; KDNode? secondChild = dim <= 0 ? node.Right : node.Left; SearchNearest(firstChild, l, a, b, depth + 1, ref minDistance, ref bestNode); if (Math.Abs(dim) < minDistance) SearchNearest(secondChild, l, a, b, depth + 1, ref minDistance, ref bestNode); } /// <summary> /// Finds the closest color for the given RGB color values. /// </summary> /// <param name="RGB">The red, green, and blue channel values.</param> /// <returns>The closest configured color, or <see langword="null"/> when the list is empty.</returns> public NamedColor? FindClosestColor((short, short, short) RGB) { (float l, float a, float b) = ColorConverter.RGBToLab(RGB); return FindClosestColor(l, a, b); } /// <summary> /// Finds the closest color for given RGB color values /// </summary> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public NamedColor? FindClosestColor(short r, short g, short b) { return FindClosestColor((r, g, b)); } /// <summary> /// Finds the closest color for given hex color value /// </summary> /// <param name="hex"></param> /// <returns></returns> public NamedColor? FindClosestColor(string hex) { (short r, short g, short b) = ColorConverter.HexToRgb(hex); return FindClosestColor(r, g, b); } /// <summary> /// Finds the closest color for a given NamedColor /// </summary> /// <param name="color"></param> /// <returns></returns> public NamedColor? FindClosestColor(NamedColor color) { return FindClosestColor(color.Rgb); } /// <summary> /// Finds the closest color for given lab color values /// </summary> /// <param name="l"></param> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public NamedColor? FindClosestColor(float l, float a, float b) { double minDistance = double.PositiveInfinity; KDNode? bestNode = null; SearchNearest(ColorTreeRoot, l, a, b, 0, ref minDistance, ref bestNode); return bestNode?.Color; } /// <summary> /// Finds the closest color for given lab color values /// </summary> /// <param name="lab"></param> /// <returns></returns> public NamedColor? FindClosestColor((float, float, float) lab) { return FindClosestColor(lab.Item1, lab.Item2, lab.Item3); } /// <summary> /// Finds the closest color name for given RGB color values /// </summary> /// <param name="RGB"></param> /// <returns></returns> public string FindClosestColorName((short, short, short) RGB) { NamedColor? color = FindClosestColor(RGB); return color?.Name ?? "Unknown"; } /// <summary> /// Finds the closest color name for given RGB color values /// </summary> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public string FindClosestColorName(short r, short g, short b) { return FindClosestColorName((r, g, b)); } /// <summary> /// Finds the closest color name for given hex color value /// </summary> /// <param name="hex"></param> /// <returns></returns> public string FindClosestColorName(string hex) { NamedColor? color = FindClosestColor(hex); return color?.Name ?? "Unknown"; } /// <summary> /// Finds the closest color name for a given NamedColor /// </summary> /// <param name="color"></param> /// <returns></returns> public string FindClosestColorName(NamedColor color) { return FindClosestColorName(color.Rgb); } /// <summary> /// Finds the closest color name for given lab color values /// </summary> /// <param name="l"></param> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public string FindClosestColorName(float l, float a, float b) { NamedColor? color = FindClosestColor(l, a, b); return color?.Name ?? "Unknown"; } /// <summary> /// Finds the closest color name for given lab color values /// </summary> /// <param name="lab"></param> /// <returns></returns> public string FindClosestColorName((float, float, float) lab) { return FindClosestColorName(lab.Item1, lab.Item2, lab.Item3); } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |