internal Image ChangeHueAndSaturation(Hsv targetHsv) { var bitmap = new Bitmap(Image); var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); byte bitsPerPixel = 32; var size = bitmapData.Stride * bitmapData.Height; byte[] data = new byte[size]; System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, data, 0, size); for (int i = 0; i < size; i += bitsPerPixel / 8) { var b = data[i]; var g = data[i + 1]; var r = data[i + 2]; var hsv = new Rgb(r, g, b).ToHsv(); var newHsv = new Hsv(targetHsv.H, targetHsv.S, hsv.V); var newRgb = newHsv.ToRgb(); data[i] = newRgb.B; data[i + 1] = newRgb.G; data[i + 2] = newRgb.R; } System.Runtime.InteropServices.Marshal.Copy(data, 0, bitmapData.Scan0, data.Length); bitmap.UnlockBits(bitmapData); return(bitmap); }
protected static List <Color> MakeRainbow(int length, int alpha, float val = 1) { List <Color> colors = new List <Color>(); for (int i = 0; i < length; i++) { Hsv color = new Hsv { H = (double)i / length * 360, S = 1, V = val }; IRgb rgb = color.ToRgb(); colors.Add(Color.FromArgb(alpha, (int)rgb.R, (int)rgb.G, (int)rgb.B)); } return(colors); }
public void ShouldConvertFromRgbToHsv() { for (var h = 0; h < 360; h++) { for (var s = 0f; s <= 1; s += 0.1f) { for (var v = 0f; v <= 1; v += 0.1f) { var hsv = new Hsv(h, s, v); var rgb = hsv.ToRgb(); var hsv2 = rgb.ToHsv(); Assert.AreEqual(hsv.H, hsv2.H); } } } }
private void DrawOutline(Graphics g, float[] heights, float scale) { List <PointF> outerEdge = GetCircularPoints(heights, scale, g); PathGradientBrush b = new PathGradientBrush(outerEdge.ToArray()); Hsv color = new Hsv { H = (double)maxAngle * 180 / Math.PI, S = 1, V = 1 }; IRgb rgb = color.ToRgb(); b.CenterColor = Color.FromArgb(255, (int)rgb.R, (int)rgb.G, (int)rgb.B); b.SurroundColors = new Color[] { Color.FromArgb(0, 0, 0, 0) }; Region rgn = new Region(); g.FillPolygon(b, outerEdge.ToArray()); }