using System; using System.Drawing; public static class Tint { // Dominant colour of the opaque, saturated pixels (hue histogram weighted by saturation), // returned as a very light tint of that colour. public static string Light(string path, double keep) { using (var b = new Bitmap(path)) { var w = new double[24]; var r = new double[24]; var g = new double[24]; var bl = new double[24]; for (int y = 0; y < b.Height; y++) for (int x = 0; x < b.Width; x++) { Color c = b.GetPixel(x, y); if (c.A < 200) continue; float s = c.GetSaturation(), l = c.GetBrightness(); if (s < 0.25f || l < 0.15f || l > 0.95f) continue; int bin = (int)(c.GetHue() / 15f) % 24; w[bin] += s; r[bin] += c.R * s; g[bin] += c.G * s; bl[bin] += c.B * s; } int best = 0; for (int i = 1; i < 24; i++) if (w[i] > w[best]) best = i; if (w[best] == 0) return "#F5F3F1"; double R = r[best] / w[best], G = g[best] / w[best], B = bl[best] / w[best]; Func t = v => (int)Math.Round(255 - (255 - v) * keep); return string.Format("#{0:X2}{1:X2}{2:X2}", t(R), t(G), t(B)); } } }