I have some C# code that is doing key-color replacement based on hue. On my local machine, it's executing perfectly. However when I push it up to the server it replaces "some" but not all of the colors. It's like it's just deciding not to run part of the code.
The images are lossless PNGs - they are intact on the server. Is this some kind of threading issue? My code is not threaded here (beyond what the webserver does), but has anyone ever seen anything similar happen?
It might also help if I mention that this code is compiled in a separate library project and then referenced from an MVC3 application.
Thanks, and here is the code sample:
private void _ReplaceImageColor(Image img, Color baseColor, Color newColor)
{
Bitmap bmp = (Bitmap)img;
double baseHue = baseColor.GetHue();
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
double pixelHue, pixelSat, pixelVal;
ColorProcessor.ColorToHSV(bmp.GetPixel(x, y), out pixelHue, out pixelSat, out pixelVal);
if (pixelHue == baseHue)
{
Color setColor = ColorProcessor.ColorFromHSV(newColor.GetHue(), pixelSat, pixelVal);
bmp.SetPixel(x, y, setColor);
}
}
}
}
Here's the methods from ColorProcessor
since people asked...
public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)
{
int max = Math.Max(color.R, Math.Max(color.G, color.B));
int min = Math.Min(color.R, Math.Min(color.G, color.B));
hue = color.GetHue();
saturation = (max == 0) ? 0 : 1d - (1d * min / max);
value = max / 255d;
}
public static Color ColorFromHSV(double hue, double saturation, double value)
{
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
double f = hue / 60 - Math.Floor(hue / 60);
value = value * 255;
int v = Convert.ToInt32(Math.Max(value, 0));
int p = Convert.ToInt32(Math.Max(value * (1 - saturation), 0));
int q = Convert.ToInt32(Math.Max(value * (1 - f * saturation), 0));
int t = Convert.ToInt32(Math.Max(value * (1 - (1 - f) * saturation), 0));
if (hi == 0)
return Color.FromArgb(255, v, t, p);
else if (hi == 1)
return Color.FromArgb(255, q, v, p);
else if (hi == 2)
return Color.FromArgb(255, p, v, t);
else if (hi == 3)
return Color.FromArgb(255, p, q, v);
else if (hi == 4)
return Color.FromArgb(255, t, p, v);
else
return Color.FromArgb(255, v, p, q);
}