static bool IsRed(Color pixel) { float Hue = pixel.GetHue(); float Saturation = pixel.GetSaturation(); float Brightness = pixel.GetBrightness(); // Hue >= 325 // Saturation >=0.25 // 0.125 < Brightness < 0.6875 before: 0.2916666 //if (Hue >= 325 && Saturation >= 0.25 && 0.26 <= Brightness && Brightness <= 0.6875) if (Hue >= 345 && Saturation >= 0.3 && 0.3 <= Brightness && Brightness <= 0.6) { return(true); // Hue < 22.59414 //Saturation >= 0.25 // 0.125 < Brightness < 0.6875 before: 0.2916666 } else if (Hue < 20 && Saturation >= 0.3 && 0.3 < Brightness && Brightness < 0.6) { return(true); } return(false); }
public static HslColor AsHsl(this Color color) { return(new HslColor() { Hue = GetHue(color), Lightness = color.GetBrightness(), Saturation = color.GetSaturation() }); }
private static Color[] ShiftHue(Color[] colorArray, Color targetColor, int count, Color averageColor) { Color[] shiftedColors = new Color[count]; float targetHue = targetColor.GetHue(); float hueRotation = targetHue - averageColor.GetHue(); float averageBrightness = averageColor.GetBrightness(); float averageSaturation = averageColor.GetSaturation(); for (int i = 0; i < count; i++) { float hue = colorArray[i].GetHue(); float brightness = colorArray[i].GetBrightness(); float saturation = colorArray[i].GetSaturation(); brightness -= averageBrightness; saturation -= averageSaturation; brightness += targetColor.GetBrightness(); saturation += targetColor.GetSaturation(); hue += hueRotation; ColorUtils.ValidateHSB(ref hue, ref saturation, ref brightness); float hueDifference = Math.Abs(targetHue - hue); if (hueDifference >= 90f) { hue = targetHue; } shiftedColors[i] = ColorUtils.FromAHSB(colorArray[i].A, hue, saturation, brightness); } return(shiftedColors); }
/// <summary> /// Generates a set of random colors where the colors differ from each other /// </summary> /// <param name="count">The amount of colors to generate</param> /// <returns></returns> public override IEnumerable <Color> GenerateMany(int count, Random rng) { Color lastColor = default(Color); for (int i = 0; i < count; i++) { Color c; //Generatea unique random color. do { c = GenerateOne(rng); } while ( //First color gets automatic pass lastColor != default(Color) && //Colors must not be similar in hue, saturation and brigtness Math.Abs(c.GetHue() - lastColor.GetHue()) < 10 && Math.Abs(c.GetSaturation() - lastColor.GetSaturation()) < 0.1f && Math.Abs(c.GetBrightness() - lastColor.GetBrightness()) < 0.1f ); lastColor = c; yield return(c); } }
public static void GetColorComponents(ColorModel colorModel, Color color, out Single componentA, out Single componentB, out Single componentC) { componentA = 0.0f; componentB = 0.0f; componentC = 0.0f; switch (colorModel) { case ColorModel.RedGreenBlue: componentA = color.R; componentB = color.G; componentC = color.B; break; case ColorModel.HueSaturationBrightness: componentA = color.GetHue(); componentB = color.GetSaturation(); componentC = color.GetBrightness(); break; case ColorModel.LabColorSpace: RGBtoLab(color.R, color.G, color.B, out componentA, out componentB, out componentC); break; case ColorModel.XYZ: RGBtoXYZ(color.R, color.G, color.B, out componentA, out componentB, out componentC); break; } }
public static Color WithNewHue(this Color color, float hue) { var saturation = color.GetSaturation(); var lightness = color.GetLightness(); return(Color.FromArgb(color.A, ColorFromHsl(hue, saturation, lightness))); }
public static Color WithNewLightness(this Color color, float lightness) { var saturation = color.GetSaturation(); var hue = color.GetHue() / 360; return(Color.FromArgb(color.A, ColorFromHsl(hue, saturation, lightness))); }
private int[,] Quantize(Bitmap bitmap, int nBinsPerChannel) { int[,] result = new int[bitmap.Width, bitmap.Height]; for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { Color pixel = bitmap.GetPixel(x, y); int brightness = (int)(nBinsPerChannel * pixel.GetBrightness()); if (brightness == nBinsPerChannel) { brightness = nBinsPerChannel - 1; } int saturation = (int)(nBinsPerChannel * pixel.GetSaturation()); if (saturation == nBinsPerChannel) { saturation = nBinsPerChannel - 1; } int hue = (int)(nBinsPerChannel * pixel.GetHue() / 360.0); if (hue == nBinsPerChannel) { hue = nBinsPerChannel - 1; } int bin = brightness + nBinsPerChannel * (saturation + nBinsPerChannel * hue); result[x, y] = bin; } } return(result); }
/// <summary> /// Shifts a color's intensity so that it looks perceptually correct when /// displayed on RGB leds. /// </summary> /// <param name="source">Unadjusted color to shift</param> /// <returns>Perceptually adjusted color</returns> public static Color ToPerceptual(this Color source) { double hue = source.GetHue() * (Math.PI / 180.0f); float intensity = source.GetIntensity() / 3.0f; float saturation = source.GetSaturation(); byte red, green, blue; if (hue < TwoPiOverThree) { red = PerceptualTransformA(hue, saturation, intensity); green = PerceptualTransformB(hue, saturation, intensity); blue = PerceptualTransformC(hue, saturation, intensity); } else if (hue < FourPiOverThree) { hue = hue - TwoPiOverThree; red = PerceptualTransformC(hue, saturation, intensity); green = PerceptualTransformA(hue, saturation, intensity); blue = PerceptualTransformB(hue, saturation, intensity); } else { hue = hue - FourPiOverThree; red = PerceptualTransformB(hue, saturation, intensity); green = PerceptualTransformC(hue, saturation, intensity); blue = PerceptualTransformA(hue, saturation, intensity); } return(Color.FromArgb(255, red, green, blue)); }
public IHlsColor GetHlsColor(Color pColor) { try { if (pColor.IsEmpty) { return(null); } IHlsColor color = null; color = new HlsColorClass { Hue = (int)pColor.GetHue(), Lightness = ((int)pColor.GetBrightness()) * 100, Saturation = ((int)pColor.GetSaturation()) * 100 }; IColor color2 = null; color2 = color; color2.Transparency = pColor.A; return(color); } catch (Exception exception) { this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.ColorFun", "GetHlsColor", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); return(null); } }
/// <summary> /// Creates a random color, but accepts a given random class instead of creating a new one. /// </summary> /// <param name="rnd"></param> /// <returns></returns> protected Color CreateRandomColor(Random rnd) { Color startColor = EditorSettings.StartColor; Color endColor = EditorSettings.EndColor; if (EditorSettings.HueSatLight) { double hLow = startColor.GetHue(); double dH = endColor.GetHue() - hLow; double sLow = startColor.GetSaturation(); double ds = endColor.GetSaturation() - sLow; double lLow = startColor.GetBrightness(); double dl = endColor.GetBrightness() - lLow; double aLow = (startColor.A) / 255.0; double da = (endColor.A - aLow) / 255.0; return(SymbologyGlobal.ColorFromHsl(rnd.NextDouble() * dH + hLow, rnd.NextDouble() * ds + sLow, rnd.NextDouble() * dl + lLow).ToTransparent((float)(rnd.NextDouble() * da + aLow))); } int rLow = Math.Min(startColor.R, endColor.R); int rHigh = Math.Max(startColor.R, endColor.R); int gLow = Math.Min(startColor.G, endColor.G); int gHigh = Math.Max(startColor.G, endColor.G); int bLow = Math.Min(startColor.B, endColor.B); int bHigh = Math.Max(startColor.B, endColor.B); int iaLow = Math.Min(startColor.A, endColor.A); int aHigh = Math.Max(startColor.A, endColor.A); return(Color.FromArgb(rnd.Next(iaLow, aHigh), rnd.Next(rLow, rHigh), rnd.Next(gLow, gHigh), rnd.Next(bLow, bHigh))); }
/// <summary> /// Creates a new instance of the HSL struct with the given System.Drawing.Color. /// </summary> /// <param name="color">A System.Drawing.Color color.</param> public HSL(Color color) : this() { Hue360 = color.GetHue(); Saturation = color.GetSaturation(); Lightness = color.GetBrightness(); A = color.A; }
/// <summary> /// Creates a list of generated colors according to the convention /// specified in the EditorSettings. /// </summary> /// <param name="count">The integer count of the number of colors to create.</param> /// <returns>The list of colors created.</returns> protected List <Color> GetColorSet(int count) { List <Color> colorRamp; if (EditorSettings.UseColorRange) { if (!EditorSettings.RampColors) { colorRamp = CreateRandomColors(count); } else if (!EditorSettings.HueSatLight) { colorRamp = CreateRampColors(count, EditorSettings.StartColor, EditorSettings.EndColor); } else { Color cStart = EditorSettings.StartColor; Color cEnd = EditorSettings.EndColor; colorRamp = CreateRampColors(count, cStart.GetSaturation(), cStart.GetBrightness(), (int)cStart.GetHue(), cEnd.GetSaturation(), cEnd.GetBrightness(), (int)cEnd.GetHue(), EditorSettings.HueShift, cStart.A, cEnd.A); } } else { colorRamp = GetDefaultColors(count); } return(colorRamp); }
internal static double GetDistanceBetweenColors(Color c1, Color c2) { var hue1 = c1.GetHue(); var hue2 = c2.GetHue(); if (hue1 > 180) { hue1 = hue1 - 360; } if (hue2 > 180) { hue2 = hue2 - 360; } var hueDifference = Math.Abs(hue1 - hue2) / 1.8; var saturation1 = c1.GetSaturation(); var saturation2 = c2.GetSaturation(); var saturationDifference = Math.Abs(saturation1 - saturation2) * 100; var brightness1 = c1.GetBrightness(); var brightness2 = c2.GetBrightness(); var brightnessDifference = Math.Abs(brightness1 - brightness2) * 100; return(hueDifference + saturationDifference + brightnessDifference); }
public static byte ClosestColor(uint color, int palette, uint[][] paletteData) { uint closestColor = paletteData[palette][0]; double diff = double.MaxValue; Color c = ColorFromUInt32(color); float targetHue = c.GetHue(); float targetSat = c.GetSaturation(); float targetBri = c.GetBrightness(); foreach (uint validColor in paletteData[palette]) { Color checkColor = ColorFromUInt32(validColor); float currentHue = checkColor.GetHue(); float currentSat = checkColor.GetSaturation(); float currentBri = checkColor.GetBrightness(); double currentDiff = Math.Pow(targetHue - currentHue, 2) + Math.Pow(targetSat - currentSat, 2) + Math.Pow(targetBri - currentBri, 2); if (currentDiff < diff) { diff = currentDiff; closestColor = validColor; } } return((byte)(Array.IndexOf(paletteData[palette], closestColor) + 1)); }
public static ImageInfo LoadBitmap(string path) { double avgBrightness = 0, avgSat = 0; Bitmap bitmap = new Bitmap(path); for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { Color c = bitmap.GetPixel(x, y); avgBrightness += c.GetBrightness(); avgSat += c.GetSaturation(); } } int numPixels = bitmap.Width * bitmap.Height; avgBrightness /= numPixels; avgSat /= numPixels; bitmap.Dispose(); System.Diagnostics.Trace.WriteLine(path + "\t" + avgBrightness + "\t" + avgSat); return(new ImageInfo() { //Bitmap = bitmap, Path = path, AvgBrightness = avgBrightness, AvgSat = avgSat }); }
public static ColorSlider Saturation(VisualDirection direction, float knobWidth, float minVisualLength, Color color, Reaction <float> changed) { return(new ColorSlider(direction, knobWidth, minVisualLength, 0f, 1f, color.GetSaturation(), new [] { Color.White, VisualHelpers.ColorFromHSB(color.GetHue(), 1f, color.GetBrightness()) }, changed)); }
/// <summary> /// Initialize a new instance of the ColorHSL class. /// </summary> /// <param name="c">Initialize from an existing Color.</param> public ColorHSL(Color c) { // Initialize from the color instance _hue = c.GetHue() / 360f; _saturation = c.GetBrightness(); _luminance = c.GetSaturation(); }
public HSLColor(Color color) { RGB = color; H = (byte)((color.GetHue() / 360.0f) * 255); S = (byte)(color.GetSaturation() * 255); L = (byte)(color.GetBrightness() * 255); }
protected Color CalculateIntermediary(Color from, Color to, float fractionDone) { if (fractionDone >= 1.0f) { return(to); } if (fractionDone <= 0.0f) { return(from); } // There are a couple of different strategies we could use here: // - Calc intermediary individual RGB components - fastest // - Calc intermediary HSB components - nicest results, but slower //Color c = Color.FromArgb( // this.CalculateIntermediary(from.R, to.R, fractionDone), // this.CalculateIntermediary(from.G, to.G, fractionDone), // this.CalculateIntermediary(from.B, to.B, fractionDone) //); Color c = FromHSB( this.CalculateIntermediary(from.GetHue(), to.GetHue(), fractionDone), this.CalculateIntermediary(from.GetSaturation(), to.GetSaturation(), fractionDone), this.CalculateIntermediary(from.GetBrightness(), to.GetBrightness(), fractionDone) ); return(Color.FromArgb(this.CalculateIntermediary(from.A, to.A, fractionDone), c)); }
public void SetColor(Color color) { Color = color; IsSettingValues = true; RSlider.Slider.Value = Color.R; GSlider.Slider.Value = Color.G; BSlider.Slider.Value = Color.B; ASlider.Slider.Value = Color.A; SSlider.Slider.Value = Color.GetSaturation(); LSlider.Slider.Value = Color.GetBrightness(); HSlider.Slider.Value = Color.GetHue(); string colorHex = Color.ToString().Remove(1, 2); hexCode.Text = colorHex; ColorDisplayBorder.Background = new SolidColorBrush(Color); IsSettingValues = false; OnPickColor?.Invoke(color); }
/// <summary> /// Creates a new dark color object for the control from the specified color and darkens it by the specified percentage. /// </summary> /// <param name="c">The <see cref="Color"/> to be darkened.</param> /// <param name="percDarker">The percentage to darken the specified <see cref="Color"/>.</param> /// <returns>A <see cref="Color"/> that represent the dark color on the control.</returns> public static Color Dark(Color c, float percDarker) { if (c.ToArgb() == SystemColors.Control.ToArgb()) { if (percDarker == 0f) { return(SystemColors.ControlDark); } if (percDarker == 1f) { return(SystemColors.ControlDarkDark); } Color controlDark = SystemColors.ControlDark; Color controlDarkDark = SystemColors.ControlDarkDark; int num = controlDark.R - controlDarkDark.R; int num2 = controlDark.G - controlDarkDark.G; int num3 = controlDark.B - controlDarkDark.B; return(Color.FromArgb((byte)(controlDark.R - ((byte)(num * percDarker))), (byte)(controlDark.G - ((byte)(num2 * percDarker))), (byte)(controlDark.B - ((byte)(num3 * percDarker))))); } float hue = c.GetHue(); float brightness = c.GetBrightness(); float saturation = c.GetSaturation(); float newBrightness = (float)(brightness - (brightness * percDarker * 1.5)); if (newBrightness < 0.0) { newBrightness = 0.0F; } return(InTheHand.Drawing.ColorInTheHand.FromHSB(hue, saturation, newBrightness)); }
/// <summary> /// Creates a new light color object for the control from the specified color and lightens it by the specified percentage. /// </summary> /// <param name="baseColor">The <see cref="Color"/> to be lightened.</param> /// <param name="percLighter">The percentage to lighten the specified <see cref="Color"/>.</param> /// <returns>Creates a new light color object for the control from the specified color and lightens it by the specified percentage.</returns> public static Color Light(Color baseColor, float percLighter) { if (baseColor.ToArgb() == SystemColors.Control.ToArgb()) { if (percLighter == 0f) { return(SystemColors.ControlLight); } if (percLighter == 1f) { return(SystemColors.ControlLightLight); } Color controlLight = SystemColors.ControlLight; Color controlLightLight = SystemColors.ControlLightLight; int num = controlLight.R - controlLightLight.R; int num2 = controlLight.G - controlLightLight.G; int num3 = controlLight.B - controlLightLight.B; return(Color.FromArgb((byte)(controlLight.R - ((byte)(num * percLighter))), (byte)(controlLight.G - ((byte)(num2 * percLighter))), (byte)(controlLight.B - ((byte)(num3 * percLighter))))); } float hue = baseColor.GetHue(); float saturation = baseColor.GetSaturation(); float brightness = baseColor.GetBrightness(); float newBrightness = (float)((brightness * percLighter * 1.5) + brightness); if (newBrightness > 1.0) { newBrightness = 1.0F; } return(ColorInTheHand.FromHSB(hue, saturation, newBrightness)); }
public HueSatLight(Color color) { Hue = color.GetHue() / 360.0; // Convert from range of 0-360 to 0.0-1.0. Lightness = color.GetBrightness(); Saturation = color.GetSaturation(); Alpha = color.A / 255.0; }
private Color SimulateGreyness(Color realColor) { double hue; double saturation; double lightness; hue = realColor.GetHue() / 360.0; if (hue != 0) { //if (System.Diagnostics.Debugger.IsAttached) //System.Diagnostics.Debugger.Break(); } saturation = realColor.GetSaturation(); lightness = realColor.GetBrightness(); double darkFactor = Math.Max(1.0 - (simulatedDarkness.Value * 0.01) * lightness, 0.0); lightness *= darkFactor; lightness = Math.Max(Math.Min(lightness, 1.0f), 0.0f); Color darkScreenColor = ColorFromHSL(hue, saturation, lightness, realColor.A); return(darkScreenColor); }
public HslColor(Color rgbColor) { this.alpha = rgbColor.A; this.Hue = rgbColor.GetHue() / 360.0F; this.Saturation = rgbColor.GetSaturation(); this.Luminance = rgbColor.GetBrightness(); }
private void button1_Click(object sender, EventArgs e) { string WallpaperPth = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", 0).ToString(); // Get the wallpaper path. System.Drawing.Bitmap btm = new System.Drawing.Bitmap(WallpaperPth); //Creating a new bitmap for the wallpaper. labelAVGLabel.Text = getDominantColor(btm).ToString(); //Get the AVG color and display it. averageColor = getDominantColor(btm); float hue = (averageColor.GetHue() + 180) % 360; // Get HSB values in order to calculate complimentary float saturation = averageColor.GetSaturation(); float brightness = averageColor.GetBrightness(); colorButton.ForeColor = averageColor; colorButton.BackColor = averageColor; HSL firstHSL = RGBToHSL(averageColor); HSL secondHSL = calculateTheOppositeHue(firstHSL); Color compColor = HSLToRGB(secondHSL); compButton.BackColor = compColor; compButton.ForeColor = compColor; labelCompColor.Text = compColor.ToString(); }
private static ColorSwatch GetColorSwatchFromJson(JsonElement json) { JsonElement colorDetailElem = json.GetProperty("page").GetProperty("colorDetail"); JsonElement colorElem = colorDetailElem.GetProperty("color"); string name = WebUtility.HtmlDecode(colorElem.GetProperty("name").GetString()); string number = colorElem.GetProperty("number").GetString(); Color color = ColorTranslator.FromHtml("#" + colorElem.GetProperty("hex").GetString()); double lrv = colorDetailElem.GetProperty("lrv").GetDouble(); return(new ColorSwatch { Name = name, ColorNumbers = new List <ColorNumber> { new ColorNumber { Number = number } }, Brand = ColorBrand.BenjaminMoore, Red = color.R, Green = color.G, Blue = color.B, Hue = color.GetHue(), Saturation = color.GetSaturation(), Lightness = color.GetBrightness(), Lrv = lrv }); }
public static float ColorNum(Color c) { var factorSat = 3; var factorBri = 3; return(c.GetSaturation() * factorSat + GetBrightness(c) * factorBri); }
private float ColorMatching(Color srcColor, Color destColor) { var sh = srcColor.GetHue(); var ss = srcColor.GetSaturation(); var sb = srcColor.GetBrightness(); var dh = destColor.GetHue(); var ds = destColor.GetSaturation(); var db = destColor.GetBrightness(); var sr = srcColor.R; var sg = srcColor.G; var sc = srcColor.B; var dr = destColor.R; var dg = destColor.G; var dc = destColor.B; float result = (float)Math.Sqrt( (Math.Abs(sh - dh) / COLOR_WEIGHT_HUE * COLOR_PROPORTION) + (Math.Abs(ss - ds) / COLOR_WEIGHT_SATURATION * COLOR_PROPORTION) + (Math.Abs(sb - db) / COLOR_WEIGHT_BRIGHTNESS * COLOR_PROPORTION) + (Math.Abs(sr - dr) / COLOR_WEIGHT_RED * COLOR_PROPORTION) + (Math.Abs(sg - dg) / COLOR_WEIGHT_GREEN * COLOR_PROPORTION) + (Math.Abs(sc - dc) / COLOR_WEIGHT_BLUE * COLOR_PROPORTION)); return(result); }
private static bool SimilarColors(Color colorOne, Color colorTwo) { if (Math.Abs(colorOne.GetHue()-colorTwo.GetHue()) <= 36) { //Console.WriteLine("Hue"); return true; } if (Math.Abs(colorOne.GetBrightness()-colorTwo.GetBrightness()) <= .1) { //Console.WriteLine("Hue"); return true; } if (Math.Abs(colorOne.GetSaturation()-colorTwo.GetSaturation()) <= .1) { //Console.WriteLine("Hue"); return true; } return false; }
public static float ColorNum(Color c) { var factorSat = 3; var factorBri = 3; return c.GetSaturation() * factorSat + GetBrightness(c) * factorBri; }
public HSLColor(Color color) { Hue = color.GetHue(); Saturation = color.GetSaturation(); Lightness = color.GetBrightness(); }
private void UpdateHsb(Color value) { Hue = (int)value.GetHue(); var brightness = value.GetBrightness(); Brightness = (int)(brightness * 100); var saturation = value.GetSaturation(); Saturation = (int)(saturation * 100); UpdateThumbPosition(); }