static Color32 GetNearestCol(int r, int g, float b) { float cr = ((float)r) * (1f / ((float)RGDetail)); float cg = ((float)g) * (1f / ((float)RGDetail)); float cb = b; // ((float)b) * (1f / ((float)gridWidth)); Color c = new Color(cr, 1f - cg, cb, 1f); if (generatePureLUT) { return(c); } float h = 0f; float s = 0f; float v = 0f; Color.RGBToHSV(c, out h, out s, out v); int bestCol = 0; float closestValue = 100f; float valueDistance = 0f; for (int i = 0; i < paletteCols.Count; i++) { valueDistance = Mathf.Sqrt((HueDistance(paletteHues[i], h) * HueDistance(paletteHues[i], h)) * HInfluence + (Mathf.Abs(paletteSaturations[i] - s) * Mathf.Abs(paletteSaturations[i] - s)) * SInfluence + (Mathf.Abs(paletteValues[i] - v) * Mathf.Abs(paletteValues[i] - v)) * VInfluence); //add RGB distances valueDistance += Mathf.Sqrt(Mathf.Abs(paletteCols[i].r - c.r) * Mathf.Abs(paletteCols[i].r - c.r) + Mathf.Abs(paletteCols[i].g - c.g) * Mathf.Abs(paletteCols[i].g - c.g) + Mathf.Abs(paletteCols[i].b - c.b) * Mathf.Abs(paletteCols[i].b - c.b)) * RGBInfluence; if (CIELabInfluence > 0f) { valueDistance += ColorMath.DeltaECIE94(paletteColsCIELab[i], ColorMath.RGBtoCIELab(new Color(cr, 1f - cg, cb))) * CIELabInfluence; } //compare if (valueDistance < closestValue) { closestValue = valueDistance; bestCol = i; } } return(paletteCols32[bestCol]); }
static void AddColorToPalette(Color32 c) { for (int i = 0; i < paletteCols32.Count; i++) { if (paletteCols32[i].r == c.r && paletteCols32[i].g == c.g && paletteCols32[i].b == c.b) { return; } } paletteCols32.Add(c); paletteCols.Add(c); float h = 0f; float s = 0f; float v = 0f; Color.RGBToHSV(paletteCols[paletteCols.Count - 1], out h, out s, out v); paletteHues.Add(h); paletteSaturations.Add(s); paletteValues.Add(v); paletteColsCIELab.Add(ColorMath.RGBtoCIELab(paletteCols[paletteCols.Count - 1])); }