/// <summary> /// Creates color from HSB color space with random hue and saturation and brighness equal to 1. /// </summary> /// <returns></returns> public static Color CreateColorWithRandomHue() { double hue = random.NextDouble() * 360; HsbColor hsbColor = new HsbColor(hue, 1, 1); return(hsbColor.ToArgbColor()); }
private void Page_Loaded(object sender, RoutedEventArgs e) { AddRandomPoint(); AddRandomPoint(); AddRandomPoint(); // these are functions that sets different properties of drawn ellipses; // these functions can be replaced by real, non-demo ones. chart.AddPropertyBindingForNamedPart<Point>("ellipse", Ellipse.FillProperty, p => { double hue = Math.Sin(1000 * (p.X + p.Y)) * 180 + 180; var color = new HsbColor(hue, 0.2, 1); return new SolidColorBrush(color.ToArgbColor()).MakeTransparent(0.7); }); chart.AddPropertyBindingForNamedPart<Point>("ellipse", Ellipse.StrokeProperty, p => { double hue = Math.Sin(1000 * (p.X + p.Y)) * 180 + 180; var color = new HsbColor(hue, 1, 1); return new SolidColorBrush(color.ToArgbColor()); }); chart.AddPropertyBindingForNamedPart<Point>("ellipse", Ellipse.RenderTransformProperty, p => new RotateTransform(120 * Math.Atan2(p.Y, p.X))); chart.AddPropertyBinding<Point>(ViewportPanel.ViewportWidthProperty, p => 0.2 * Math.Abs(Math.Sin(p.X))); chart.AddPropertyBinding<Point>(ViewportPanel.ViewportHeightProperty, p => 0.2 * Math.Abs(Math.Cos(p.X))); DataContext = points; }
public static SolidColorBrush MakeDarker(this SolidColorBrush brush, double darknessDelta) { Color color = brush.Color; HsbColor hsbColor = HsbColor.FromArgb(color); hsbColor.Brightness -= darknessDelta; SolidColorBrush result = new SolidColorBrush(hsbColor.ToArgb()); return(result); }
/// <summary> /// Creates HSBColor from the ARGB color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static HsbColor FromArgbColor(Color color) { double limit255 = 255; double r = color.R / limit255; double g = color.G / limit255; double b = color.B / limit255; double max = Math.Max(Math.Max(r, g), b); double min = Math.Min(Math.Min(r, g), b); double len = max - min; double brightness = max; // 0.5 * (max + min); double sat; double hue; if (max == 0 || len == 0) { sat = hue = 0; } else { sat = len / max; if (r == max) { hue = (g - b) / len; } else if (g == max) { hue = 2 + (b - r) / len; } else { hue = 4 + (r - g) / len; } } hue *= 60; if (hue < 0) { hue += 360; } HsbColor res = new HsbColor(); res.hue = hue; res.saturation = sat; res.brightness = brightness; res.alpha = color.A / limit255; return(res); }
public static Color[] CreateRandomColors(int colorNum) { double startHue = random.NextDouble() * 360; Color[] res = new Color[colorNum]; double hueStep = 360.0 / colorNum; for (int i = 0; i < res.Length; i++) { double hue = startHue + i * hueStep; res[i] = new HsbColor(hue, 1, 1).ToArgb(); } return res; }
public static Color[] CreateRandomColors(int colorNum) { double startHue = random.NextDouble() * 360; Color[] res = new Color[colorNum]; double hueStep = 360.0 / colorNum; for (int i = 0; i < res.Length; i++) { double hue = startHue + i * hueStep; res[i] = new HsbColor(hue, 1, 1).ToArgbColor(); } return(res); }
/// <summary> /// Determines whether the specified <see cref="System.Object"/> is equal to this instance. /// </summary> /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { if (obj is HsbColor) { HsbColor c = (HsbColor)obj; return(c.alpha == alpha && c.brightness == brightness && c.hue == hue && c.saturation == saturation); } else { return(false); } }
public static SolidColorBrush ChangeSaturation(this SolidColorBrush brush, double saturationFactor) { Color color = brush.Color; HsbColor hsbColor = HsbColor.FromArgbColor(color); hsbColor.Saturation *= saturationFactor; if (hsbColor.Saturation > 1.0) { hsbColor.Saturation = 1.0; } SolidColorBrush result = new SolidColorBrush(hsbColor.ToArgbColor()); return(result); }
public static SolidColorBrush ChangeLightness(this SolidColorBrush brush, double lightnessFactor) { Color color = brush.Color; HsbColor hsbColor = HsbColor.FromArgbColor(color); hsbColor.Brightness *= lightnessFactor; if (hsbColor.Brightness > 1.0) { hsbColor.Brightness = 1.0; } SolidColorBrush result = new SolidColorBrush(hsbColor.ToArgbColor()); return(result); }
private EnumerableDataSource<DataPoint> CreateDataSource(IEnumerable<DataPoint> data) { EnumerableDataSource<DataPoint> ds = new EnumerableDataSource<DataPoint>(data); MercatorTransform transform = new MercatorTransform(); ds.SetXMapping(p => p.X); ds.SetYMapping(p => transform.DataToViewport(new Point(0, p.Y)).Y); ds.AddMapping(CirclePointMarker.FillProperty, dp => { double alpha = (dp.Data - currentRange.Min) / (currentRange.Max - currentRange.Min); Debug.Assert(0 <= alpha && alpha <= 1); const double hueWidth = 100; double hue = hueWidth * (alpha - 0.5) + hueSlider.Value; if (hue > 360) hue -= 360; else if (hue < 0) hue += 360; Debug.Assert(0 <= hue && hue <= 360); Color mainColor = new HsbColor(hue, 1, 0 + 1 * alpha, 0.3 + 0.7 * alpha).ToArgbColor(); const int colorCount = 5; GradientStopCollection colors = new GradientStopCollection(colorCount); double step = 1.0 / (colorCount - 1); for (int i = 0; i < colorCount; i++) { Color color = mainColor; double x = attSlider.Value * step * i; color.A = (byte)(255 * Math.Exp(-x * x)); colors.Add(new GradientStop(color, step * i)); } return new RadialGradientBrush(colors); }); return ds; }
private List<Vertex.PositionColored> BuildVertices(PointSet pointSet, double altitude) { List<Vertex.PositionColored> vertices = new List<Vertex.PositionColored>(); double minT = 0, maxT = 0, k; if (!Double.IsNaN(pointSet.MaxValue)) { minT = pointSet.MinValue; maxT = pointSet.MaxValue; k = 1.0 / (maxT - minT); } else k = Double.NaN; for (int j = 0; j < pointSet.Data.Count; j++) { float x = (float)pointSet.Data[j].Longitude; float y = (float)pointSet.Data[j].Latitude; LatLonAlt position = LatLonAlt.CreateUsingDegrees(y + step, x + step, altitude); Vector3F vec = new Vector3F(position.GetVector()); double hue; Color color = Color.Red; System.Windows.Media.Color tempColor; if (!Double.IsNaN(k)) { hue = 270 * ((maxT - (double)pointSet.Data[j].Value) * k); tempColor = new HsbColor(hue, 1, 1, 0.8).ToArgb(); color = Color.FromArgb(tempColor.A, tempColor.R, tempColor.G, tempColor.B); } vertices.Add(new Vertex.PositionColored(vec, color.ToArgb())); position = LatLonAlt.CreateUsingDegrees(y - step, x - step, altitude); vec = new Vector3F(position.GetVector()); vertices.Add(new Vertex.PositionColored(vec, color.ToArgb())); position = LatLonAlt.CreateUsingDegrees(y + step, x - step, altitude); vec = new Vector3F(position.GetVector()); vertices.Add(new Vertex.PositionColored(vec, color.ToArgb())); position = LatLonAlt.CreateUsingDegrees(y - step, x + step, altitude); vec = new Vector3F(position.GetVector()); vertices.Add(new Vertex.PositionColored(vec, color.ToArgb())); } return vertices; }
public static HsbColor ToHsbColor(this Color color) { return(HsbColor.FromArgbColor(color)); }
public static HsbColor FromArgb(Color color) { double limit = 255; double r = color.R / limit; double g = color.G / limit; double b = color.B / limit; double max = Math.Max(Math.Max(r, g), b); double min = Math.Min(Math.Min(r, g), b); double len = max - min; double brightness = max; // 0.5 * (max + min); double sat; double hue; if (max == 0 || len == 0) { sat = hue = 0; } else { sat = len / max; if (r == max) { hue = (g - b) / len; } else if (g == max) { hue = 2 + (b - r) / len; } else { hue = 4 + (r - g) / len; } } hue *= 60; if (hue < 0) { hue += 360; } //hue = max == min ? 0 : // (max == r && g >= b) ? 60 * (g - b) / len : // (max == r && g < b) ? 60 * (g - b) / len + 360 : // max == g ? 60 * (b - r) / len + 120 : 60 * (r - g) / len + 240; //sat = len / brightness; //sat = (sat == 0 || max == min) ? 0 : // (0 <= sat && sat <= 0.5) ? len / (max + min) : // (0.5 < sat && sat < 1) ? len / (2 - 2 * sat) : 1; HsbColor res = new HsbColor(); res.hue = hue; res.saturation = sat; res.brightness = brightness; res.alpha = color.A / limit; return(res); }
public RasterPatch2 GetTilePatch(PointSet pointSet, Box2 regionBox, double iconSize) { GeoRect tileBox = new GeoRect( regionBox.MinCoordinate.X, regionBox.MinCoordinate.Y, regionBox.MaxCoordinate.X - regionBox.MinCoordinate.X, regionBox.MaxCoordinate.Y - regionBox.MinCoordinate.Y); int width = 256; int height = 256; Bitmap resultBitmap = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(resultBitmap); double minT = 0, maxT = 0, k; if (!Double.IsNaN(pointSet.MaxValue)) { minT = pointSet.MinValue; maxT = pointSet.MaxValue; k = 1.0 / (maxT - minT); } else k = Double.NaN; ColorMatrix colorMatrix = new ColorMatrix(); ImageAttributes imageAttributes = new ImageAttributes(); for (int i = 0; i < pointSet.Data.Count; i++) { GeoRect workingRect = new GeoRect( pointSet.Data[i].Longitude - iconSize / 2.0, pointSet.Data[i].Latitude - iconSize / 2.0, iconSize, iconSize); if (GeoRect.IntersectionExist(workingRect, tileBox)) { int x0 = (int)(Math.Min(width, Math.Max(0, pointSet.Data[i].Longitude - iconSize / 2.0 - tileBox.Left) / tileBox.Width * width)); int y0 = (int)(Math.Min(height, Math.Max(0, tileBox.Top - pointSet.Data[i].Latitude - iconSize / 2.0) / tileBox.Height * height)); int x1 = (int)(Math.Min(width, Math.Max(0, pointSet.Data[i].Longitude + iconSize / 2.0 - tileBox.Left) / tileBox.Width * width)); int y1 = (int)(Math.Min(height, Math.Max(0, tileBox.Top - pointSet.Data[i].Latitude + iconSize / 2.0) / tileBox.Height * height)); double widthX = Math.Min(tileBox.Width, iconSize); double heightY = Math.Min(tileBox.Height, iconSize); lock (icon) { System.Windows.Media.Color tempColor = System.Windows.Media.Brushes.Red.Color; if (!Double.IsNaN(k)) { double hue = 270 * ((maxT - (double)pointSet.Data[i].Value) * k); tempColor = new HsbColor(hue, 1, 1, 0.8).ToArgb(); } colorMatrix.Matrix00 = (float)tempColor.R / 255f; colorMatrix.Matrix11 = (float)tempColor.G / 255f; colorMatrix.Matrix22 = (float)tempColor.B / 255f; imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int x2 = 0; if (pointSet.Data[i].Longitude - iconSize / 2.0 < tileBox.Left) { x2 = icon.Width - (int)((double)(x1 - x0) * icon.Width / (widthX / tileBox.Width * width)); } int y2 = 0; if (tileBox.Top - pointSet.Data[i].Latitude - iconSize / 2.0 < 0) { y2 = icon.Height - (int)((double)(y1 - y0) * icon.Height / (heightY / tileBox.Height * height)); } graphics.DrawImage( icon, new Rectangle(x0, y0, x1 - x0, y1 - y0), x2, y2, (int)((double)(x1 - x0) * icon.Width / (widthX / tileBox.Width * width)), (int)((double)(y1 - y0) * icon.Height / (heightY / tileBox.Height * height)), GraphicsUnit.Pixel, imageAttributes); } } } return new RasterPatch2( regionBox, resultBitmap, Wgs84CoordinateReferenceSystem.Instance); }
/// <summary> /// Creates HSBColor from the ARGB color. /// </summary> /// <param name="color">The color.</param> /// <returns></returns> public static HsbColor FromArgbColor(Color color) { double limit255 = 255; double r = color.R / limit255; double g = color.G / limit255; double b = color.B / limit255; double max = Math.Max(Math.Max(r, g), b); double min = Math.Min(Math.Min(r, g), b); double len = max - min; double brightness = max; // 0.5 * (max + min); double sat; double hue; if (max == 0 || len == 0) { sat = hue = 0; } else { sat = len / max; if (r == max) { hue = (g - b) / len; } else if (g == max) { hue = 2 + (b - r) / len; } else { hue = 4 + (r - g) / len; } } hue *= 60; if (hue < 0) hue += 360; HsbColor res = new HsbColor(); res.hue = hue; res.saturation = sat; res.brightness = brightness; res.alpha = color.A / limit255; return res; }
public static HsbColor FromArgb(Color color) { double limit = 255; double r = color.R / limit; double g = color.G / limit; double b = color.B / limit; double max = Math.Max(Math.Max(r, g), b); double min = Math.Min(Math.Min(r, g), b); double len = max - min; double brightness = max; // 0.5 * (max + min); double sat; double hue; if (max == 0 || len == 0) { sat = hue = 0; } else { sat = len / max; if (r == max) { hue = (g - b) / len; } else if (g == max) { hue = 2 + (b - r) / len; } else { hue = 4 + (r - g) / len; } } hue *= 60; if (hue < 0) hue += 360; //hue = max == min ? 0 : // (max == r && g >= b) ? 60 * (g - b) / len : // (max == r && g < b) ? 60 * (g - b) / len + 360 : // max == g ? 60 * (b - r) / len + 120 : 60 * (r - g) / len + 240; //sat = len / brightness; //sat = (sat == 0 || max == min) ? 0 : // (0 <= sat && sat <= 0.5) ? len / (max + min) : // (0.5 < sat && sat < 1) ? len / (2 - 2 * sat) : 1; HsbColor res = new HsbColor(); res.hue = hue; res.saturation = sat; res.brightness = brightness; res.alpha = color.A / limit; return res; }
private void CreateNoizeTexture(GraphicsDevice device) { const int size = 512; noizeTexture = new Texture2D(device, size, size); int[] noizeData = new int[size * size]; Random rnd = new Random(); for (int i = 0; i < size * size; i++) { HsbColor color = new HsbColor(0, 0, Math.Round(5 * rnd.NextDouble()) / 4); int argb = color.ToArgb(); noizeData[i] = argb; } noizeTexture.SetData(noizeData); }
/// <summary> /// Creates color from HSB color space with random hue and saturation and brighness equal to 1. /// </summary> /// <returns></returns> public static Color CreateColorWithRandomHue() { double hue = random.NextDouble() * 360; HsbColor hsbColor = new HsbColor(hue, 1, 1); return hsbColor.ToArgb(); }