/// <summary> /// Highlight the specified icon with the specified color. /// </summary> /// <param name="icn">Input icon.</param> /// <param name="liteColor">Highlight base color.</param> /// <returns>A new highlighted Image object.</returns> /// <remarks></remarks> public static Image HiliteIcon(Icon icn, Color liteColor) { var n = new Bitmap(icn.Width, icn.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var g = System.Drawing.Graphics.FromImage(n); int lc = liteColor.ToArgb(); g.FillRectangle(Brushes.Transparent, new Rectangle(0, 0, n.Width, n.Height)); g.DrawIcon(icn, 0, 0); g.Dispose(); var bm = new System.Drawing.Imaging.BitmapData(); n.LockBits(new Rectangle(0, 0, n.Width, n.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb, bm); int[] b; int i; int c; b = new int[(bm.Width * bm.Height)]; // take the unmanaged memory and make it something manageable and VB-like. Marshal.Copy(bm.Scan0, b, 0, bm.Stride * bm.Height); // NativeLib.Native.MemCpy(bm.Scan0, b, bm.Stride * bm.Height) c = b.Length; int stp = (int)(bm.Stride / (double)bm.Width); var hsv = new HSVDATA(); var hsv2 = new HSVDATA(); // convert the color to HSV. ColorMath.ColorToHSV(liteColor, ref hsv); for (i = 0; i < c; i++) { if (b[i] == 0) { continue; } ColorMath.ColorToHSV(Color.FromArgb(b[i]), ref hsv2); hsv2.Hue = hsv.Hue; hsv2.Saturation = hsv.Saturation; hsv2.Value *= 1.1d; b[i] = ColorMath.HSVToColor(hsv2); } Marshal.Copy(b, 0, bm.Scan0, bm.Stride * bm.Height); n.UnlockBits(bm); return(n); }
private void SetSelectedColor(UniColor?selc = null) { UniColor clr; if (selc != null) { clr = (UniColor)selc; SelectedColor = Color.FromArgb(clr.A, clr.R, clr.G, clr.B); return; } else if (SelectedColor == null) { SelectedColorName = null; Point.Visibility = Surround.Visibility = Visibility.Hidden; HuePicker.Visibility = Visibility.Hidden; return; } clr = ((Color)SelectedColor).GetUniColor(); var nc = NamedColor.FindColor(clr, NameResolution == ColorNameResolution.Closest); if (nc != null) { SelectedColorName = nc.Name; } else { SelectedColorName = clr.ToString(UniColorFormatOptions.HexRgbWebFormat); } if (cpRender == null) { return; } HSVDATA hsv1 = ColorToHSV(clr); HSVDATA hsv2; HSVDATA hsv3; HSVDATA? hsv4 = null; HSVDATA? hsv5 = null; UniColor uc; ColorPickerElement cel = new ColorPickerElement(); foreach (var c in cpRender.Elements) { uc = c.Color; hsv2 = ColorToHSV(uc); hsv3 = (hsv1 - hsv2).Abs(); if (hsv4 == null) { hsv4 = hsv3; } else if (hsv3 < hsv4) { hsv5 = hsv2; hsv4 = hsv3; cel = c; } if (selc == uc) { cel = c; break; } } if (Mode == ColorPickerMode.HueWheel && !double.IsNaN(ActualWidth) && !double.IsNaN(ActualHeight) && ActualWidth != -1 && ActualHeight != -1) { if (hsv5 is HSVDATA hsv) { Point.Visibility = Surround.Visibility = Visibility.Hidden; HuePicker.Visibility = Visibility.Visible; int hp = HuePointerSize; PolarCoordinates pc = new PolarCoordinates(); double arc = hsv.Hue - HueOffset; if (arc < 0) { arc += 360; } int rad; int h = (int)ActualHeight, w = (int)ActualWidth; if (h < w) { rad = h / 2; w = h; } else { rad = w / 2; h = w; } pc.Arc = arc; pc.Radius = rad; var lc = pc.ToScreenCoordinates(new Rect(0, 0, w, h)); if (lc.X < (w / 2)) { lc.X -= hp; } if (lc.Y < (h / 2)) { lc.Y -= hp; } HuePicker.SetValue(Canvas.LeftProperty, lc.X); HuePicker.SetValue(Canvas.TopProperty, lc.Y); HueSize.ScaleX = (HuePointerSize / 5); HueSize.ScaleY = (HuePointerSize / 5); HueAngle.Angle = pc.Arc; } } else { Point.Visibility = Surround.Visibility = Visibility.Visible; HuePicker.Visibility = Visibility.Hidden; Point.SetValue(Canvas.LeftProperty, (double)cel.Center.X); Point.SetValue(Canvas.TopProperty, (double)cel.Center.Y); Surround.SetValue(Canvas.LeftProperty, (double)cel.Center.X - 8); Surround.SetValue(Canvas.TopProperty, (double)cel.Center.Y - 8); Surround.Stroke = Point.Stroke = new SolidColorBrush((Color)SelectedColor); selectedElement = cel; } }