public static byte[] TransformHSV(byte[] rgbBuffer) { byte[] hsvBuffer = new byte[rgbBuffer.Length]; // BMP header length = 54 for (int i = 54; i < rgbBuffer.Length; i += 3) { RGBType rgb; rgb.B = rgbBuffer[i]; rgb.G = rgbBuffer[i + 1]; rgb.R = rgbBuffer[i + 2]; HSVType hsv = RGB_To_HSV(rgb); HSV_Quantize(ref hsv); rgb = HSV_To_RGB(hsv); hsvBuffer[i] = (byte)Math.Round(rgb.B); hsvBuffer[i + 1] = (byte)Math.Round(rgb.G); hsvBuffer[i + 2] = (byte)Math.Round(rgb.R); } return(hsvBuffer); }
static void HSV_Quantize(ref HSVType hsv) { //hsv.H = (float)((int)((hsv.H * 60.0) / 10.0) / 6.0); hsv.H = (float)((int)((hsv.H * 60.0) / 20.0) / 3.0); }
static RGBType HSV_To_RGB(HSVType hsv) { float h = (float)hsv.H; float s = (float)hsv.S; float v = (float)hsv.V; float m, n, f; RGBType rgb; rgb.R = rgb.G = rgb.B = 0; // initial int i; if (h == -1) { rgb.R = rgb.G = rgb.B = v; } i = (int)Math.Floor(h); f = h - i; //if (!(i & 1)) if (i % 2 == 0) {// if i is even f = 1 - f; } m = v * (1 - s); n = v * (1 - s * f); switch (i) { case 6: case 0: rgb.R = v; rgb.G = n; rgb.B = m; break; case 1: rgb.R = n; rgb.G = v; rgb.B = m; break; case 2: rgb.R = m; rgb.G = v; rgb.B = n; break; case 3: rgb.R = m; rgb.G = n; rgb.B = v; break; case 4: rgb.R = n; rgb.G = m; rgb.B = v; break; case 5: rgb.R = v; rgb.G = m; rgb.B = n; break; } rgb.R *= 255; rgb.G *= 255; rgb.B *= 255; return(rgb); }