public HSVfloat RGBtoHSV(RGBfloat rgb) { HSVfloat hsv = new HSVfloat(); float min = minValue(minValue(rgb.red, rgb.green), rgb.blue); float max = maxValue(maxValue(rgb.red, rgb.green), rgb.blue); float chroma = max - min; hsv.hue = 0; hsv.sat = 0; if (chroma != 0.0f) { if (rgb.red == max) { hsv.hue = (rgb.green - rgb.blue) / chroma; if (hsv.hue < 0.0f) { hsv.hue += 6.0f; } } else if (rgb.green == max) { hsv.hue = ((rgb.blue - rgb.red) / chroma) + 2.0f; } else { hsv.hue = ((rgb.red - rgb.green) / chroma) + 4.0f; } hsv.hue *= 60.0f; hsv.sat = chroma / max; } hsv.val = max; return(hsv); }
public void init(Image image, int sliceNumber) { _height = image.Height; Bitmap slice = new Bitmap(image); Color[] pixelColumn = new Color[slice.Height]; for (int i = 0; i < slice.Height; ++i) { pixelColumn[i] = slice.GetPixel(sliceNumber, i); } for (int i = 0; i < _height; i++) { _averageRed += pixelColumn[i].R; _averageGreen += pixelColumn[i].G; _averageBlue += pixelColumn[i].B; } _averageRed = _averageRed /= _height; _averageGreen = _averageGreen /= _height; _averageBlue = _averageBlue /= _height; RGBfloat rgb; rgb.red = _averageRed / 255; rgb.green = _averageGreen / 255; rgb.blue = _averageBlue / 255; HSVfloat hsv = util.RGBtoHSV(rgb); _averageHue = (hsv.hue / 360) * 255; _averageSat = hsv.sat * 255; _averageVal = hsv.val * 255; }