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 void ColorToHSV(Color color, out float hue, out float saturation, out float value) { float max = Math.Max(color.r, Math.Max(color.g, color.b)); float min = Math.Min(color.r, Math.Min(color.g, color.b)); hue = color.GetHue(); saturation = (max == 0) ? 0 : 1f - (1f * min / max); value = max; }
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 HSL ToHSL() { Color color = ToColor(); return(new HSL(color.GetHue(), color.GetSaturation() * 100, color.GetBrightness() * 100)); }
public static float GetHue(this Color32 color) { Color c = color; return(c.GetHue()); }
/// <summary> /// Repaints the form with cool background and stuff /// </summary> /// <param name="graph">The graphics object to paint to, the element will be drawn to 0, 0</param> public virtual void Paint(Graphics graph) { //Sets up the colors to use Pen outlinePen = new Pen(SymbologyGlobal.ColorFromHsl(Color.GetHue(), Color.GetSaturation(), Color.GetBrightness() * 0.6 * Highlight), 1.75F); Color gradientTop = SymbologyGlobal.ColorFromHsl(Color.GetHue(), Color.GetSaturation(), Color.GetBrightness() * 0.7 * Highlight); Color gradientBottom = SymbologyGlobal.ColorFromHsl(Color.GetHue(), Color.GetSaturation(), Color.GetBrightness() * 1.0 * Highlight); //The path used for drop shadows GraphicsPath shadowPath = new GraphicsPath(); ColorBlend colorBlend = new ColorBlend(3); colorBlend.Colors = new[] { Color.Transparent, Color.FromArgb(180, Color.DarkGray), Color.FromArgb(180, Color.DimGray) }; colorBlend.Positions = new[] { 0f, 0.125f, 1f }; //Draws Rectangular Shapes if (Shape == ModelShape.Rectangle) { //Draws the shadow shadowPath.AddPath(GetRoundedRect(new Rectangle(5, 5, Width, Height), 10), true); PathGradientBrush shadowBrush = new PathGradientBrush(shadowPath); shadowBrush.WrapMode = WrapMode.Clamp; shadowBrush.InterpolationColors = colorBlend; graph.FillPath(shadowBrush, shadowPath); //Draws the basic shape Rectangle fillRectange = new Rectangle(0, 0, Width - 5, Height - 5); GraphicsPath fillArea = GetRoundedRect(fillRectange, 5); LinearGradientBrush myBrush = new LinearGradientBrush(fillRectange, gradientBottom, gradientTop, LinearGradientMode.Vertical); graph.FillPath(myBrush, fillArea); graph.DrawPath(outlinePen, fillArea); //Draws the status light DrawStatusLight(graph); //Draws the text SizeF textSize = graph.MeasureString(Name, Font, Width); RectangleF textRect; if ((textSize.Width < Width) || (textSize.Height < Height)) { textRect = new RectangleF((Width - textSize.Width) / 2, (Height - textSize.Height) / 2, textSize.Width, textSize.Height); } else { textRect = new RectangleF(0, (Height - textSize.Height) / 2, Width, textSize.Height); } graph.DrawString(Name, Font, new SolidBrush(Color.FromArgb(50, Color.Black)), textRect); textRect.X = textRect.X - 1; textRect.Y = textRect.Y - 1; graph.DrawString(Name, Font, Brushes.Black, textRect); //Garbage collection fillArea.Dispose(); myBrush.Dispose(); } //Draws Ellipse Shapes if (Shape == ModelShape.Ellipse) { //Draws the shadow shadowPath.AddEllipse(0, 5, Width + 5, Height); PathGradientBrush shadowBrush = new PathGradientBrush(shadowPath); shadowBrush.WrapMode = WrapMode.Clamp; shadowBrush.InterpolationColors = colorBlend; graph.FillPath(shadowBrush, shadowPath); //Draws the Ellipse Rectangle fillArea = new Rectangle(0, 0, Width, Height); LinearGradientBrush myBrush = new LinearGradientBrush(fillArea, gradientBottom, gradientTop, LinearGradientMode.Vertical); graph.FillEllipse(myBrush, 1, 1, Width - 5, Height - 5); graph.DrawEllipse(outlinePen, 1, 1, Width - 5, Height - 5); //Draws the text SizeF textSize = graph.MeasureString(Name, Font, Width); RectangleF textRect; if ((textSize.Width < Width) || (textSize.Height < Height)) { textRect = new RectangleF((Width - textSize.Width) / 2, (Height - textSize.Height) / 2, textSize.Width, textSize.Height); } else { textRect = new RectangleF(0, (Height - textSize.Height) / 2, Width, textSize.Height); } graph.DrawString(Name, Font, new SolidBrush(Color.FromArgb(50, Color.Black)), textRect); textRect.X = textRect.X - 1; textRect.Y = textRect.Y - 1; graph.DrawString(Name, Font, Brushes.Black, textRect); //Garbage collection myBrush.Dispose(); } //Draws Triangular Shapes if (Shape == ModelShape.Triangle) { //Draws the shadow Point[] ptShadow = new Point[4]; ptShadow[0] = new Point(5, 5); ptShadow[1] = new Point(Width + 5, ((Height - 5) / 2) + 5); ptShadow[2] = new Point(5, Height + 2); ptShadow[3] = new Point(5, 5); shadowPath.AddLines(ptShadow); PathGradientBrush shadowBrush = new PathGradientBrush(shadowPath); shadowBrush.WrapMode = WrapMode.Clamp; shadowBrush.InterpolationColors = colorBlend; graph.FillPath(shadowBrush, shadowPath); //Draws the shape Point[] pt = new Point[4]; pt[0] = new Point(0, 0); pt[1] = new Point(Width - 5, (Height - 5) / 2); pt[2] = new Point(0, Height - 5); pt[3] = new Point(0, 0); GraphicsPath myPath = new GraphicsPath(); myPath.AddLines(pt); Rectangle fillArea = new Rectangle(1, 1, Width - 5, Height - 5); LinearGradientBrush myBrush = new LinearGradientBrush(fillArea, gradientBottom, gradientTop, LinearGradientMode.Vertical); graph.FillPath(myBrush, myPath); graph.DrawPath(outlinePen, myPath); //Draws the text SizeF textSize = graph.MeasureString(Name, Font, Width); RectangleF textRect; if ((textSize.Width < Width) || (textSize.Height < Height)) { textRect = new RectangleF((Width - textSize.Width) / 2, (Height - textSize.Height) / 2, textSize.Width, textSize.Height); } else { textRect = new RectangleF(0, (Height - textSize.Height) / 2, Width, textSize.Height); } graph.DrawString(Name, Font, Brushes.Black, textRect); //Garbage collection myBrush.Dispose(); } //Garbage collection shadowPath.Dispose(); outlinePen.Dispose(); }
/// <summary>init from Color</summary> public ColorReal(Color color) : this(color.GetHue(), color.GetSaturation(), color.GetBrightness()) { }
public HSL(Color c) : this(c.GetHue() / 360.0, c.GetSaturation(), c.GetBrightness()) { }
public double[] RgbToHsl(Color color) // RGB TO HSL { double hue = color.GetHue(), saturation, luminance; // Convert RGB to a 0.0 to 1.0 range. double tempR = color.R / 255.0; double tempG = color.G / 255.0; double tempB = color.B / 255.0; // Get the maximum and minimum RGB components. double max = tempR; if (max < tempG) { max = tempG; } if (max < tempB) { max = tempB; } double min = tempR; if (min > tempG) { min = tempG; } if (min > tempB) { min = tempB; } double diff = max - min; luminance = (max + min) / 2; if (Math.Abs(diff) < 0.00001) { saturation = 0; } else { if (luminance <= 0.5) { saturation = diff / (max + min); } else { saturation = (max == 0) ? 0 : 1d - (1d * min / max); } } double[] temp = new double[] { hue, saturation, luminance }; for (int i = 1; i < temp.Length; i++) { if (temp[i] < 0) { temp[i] = 0; } else if (temp[i] > 1) { temp[i] = 1; } } return(temp); }
public void GenerateNextLayer() { float hue = currentColor.GetHue() + ColorHueStep; if (hue > 360f) { hue = 0; } currentColor = ColorHelper.FromAhsb( currentColor.A, hue, currentColor.GetSaturation(), currentColor.GetBrightness()); List <Prism> newLayer = new List <Prism>(currentLayer.Count); foreach (Prism prism in currentLayer) { Vector3 oldUp = Vector3.Cross( Vector3.Normalize(prism.Base), Vector3.Normalize(prism.Depth)); Vector3 newUp = Vector3.Cross( Vector3.Normalize(prism.Apex), Vector3.Normalize(prism.Depth)); Matrix oldBasis = new Matrix( prism.Base.X, prism.Base.Y, prism.Base.Z, 0, prism.Depth.X, prism.Depth.Y, prism.Depth.Z, 0, oldUp.X, oldUp.Y, oldUp.Z, 0, 0, 0, 0, 1); oldBasis = Matrix.Invert(oldBasis); Vector3 oldApexInOldBasis = Vector3.TransformNormal(prism.Apex, ref oldBasis); Vector3 newUpInOldBasis = Vector3.TransformNormal(newUp, ref oldBasis); Matrix transformation = new Matrix( 0f, oldApexInOldBasis.X, newUpInOldBasis.X, 0, -1f, oldApexInOldBasis.Y, newUpInOldBasis.Y, 0, 0f, oldApexInOldBasis.Z, newUpInOldBasis.Z, 0, 0, 0, 0, 1); transformation = Matrix.Transpose(transformation); transformation = Matrix.Invert(transformation); Matrix returnBasis = new Matrix( -prism.Depth.X, prism.Apex.X, newUp.X, 0, -prism.Depth.Y, prism.Apex.Y, newUp.Y, 0, -prism.Depth.Z, prism.Apex.Z, newUp.Z, 0, 0, 0, 0, 1); returnBasis = Matrix.Transpose(returnBasis); Matrix resultTransform = oldBasis * transformation * returnBasis; Prism newPrism = new Prism { Position = prism.Position + newUp * prism.Apex.Length, Base = Vector3.TransformNormal(prism.Base, ref resultTransform), Depth = Vector3.TransformNormal(prism.Depth, ref resultTransform), Apex = Vector3.TransformNormal(prism.Apex, ref resultTransform), }; newLayer.Add(newPrism); polygons.AddRange(ModelBuilder.CreateParallelepiped( newPrism.Position, newPrism.Apex, prism.Position - newPrism.Position, newPrism.Depth, currentColor)); } currentLayer = newLayer; LayerCount++; }
private void SetEndHsl() { _endHue = (int)_endColor.GetHue(); _endSat = _endColor.GetSaturation(); _endLight = _endColor.GetBrightness(); }
private void SetStartHsl() { _startHue = (int)_startColor.GetHue(); _startSat = _startColor.GetSaturation(); _startLight = _startColor.GetBrightness(); }
private static Color[] ShiftHue(Color[] colorArray, Color targetColor, int count, bool isZora, bool isGradient, bool isFierceDeity) { Color[] shiftedColors = new Color[count]; Color averageColor; if (isZora && !isGradient) { averageColor = ColorUtils.GetAverageColour(colorArray, 16); } else { averageColor = ColorUtils.GetAverageColour(colorArray, count); } float hueRotation = targetColor.GetHue() - averageColor.GetHue(); float averageBrightness = averageColor.GetBrightness(); float averageSaturation = averageColor.GetSaturation(); for (int i = 0; i < count; i++) { if ((i == 12) && (count == 14)) { shiftedColors[i] = colorArray[i]; continue; } 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; if (isFierceDeity) { saturation = targetColor.GetSaturation(); hue = targetColor.GetHue(); } if (isZora && isGradient) { if (i > 351) { float x0 = colorArray[352].GetBrightness(); float x1 = colorArray[511].GetBrightness(); float x = colorArray[i].GetBrightness(); hue = targetColor.GetHue(); saturation = targetColor.GetSaturation(); brightness = Interpolate(targetColor.GetBrightness(), colorArray[511].GetBrightness(), x0, x1, x); } } ColorUtils.ValidateHSB(ref hue, ref saturation, ref brightness); shiftedColors[i] = ColorUtils.FromAHSB(colorArray[i].A, hue, saturation, brightness); //this code is a mess if (isZora && isGradient) { if (i < 96) { shiftedColors[i] = colorArray[i]; } else if (i < 352) { float x0 = colorArray[95].GetBrightness(); float x1 = colorArray[352].GetBrightness(); float x = colorArray[i].GetBrightness(); int rr = (int)Interpolate(colorArray[95].R, targetColor.R, x0, x1, x); int gg = (int)Interpolate(colorArray[95].G, targetColor.G, x0, x1, x); int bb = (int)Interpolate(colorArray[95].B, targetColor.B, x0, x1, x); ColorUtils.ValidateRGB(ref rr, ref gg, ref bb); shiftedColors[i] = Color.FromArgb(rr, gg, bb); } } else if (isZora) { if (i > 15) { float x0 = colorArray[14].GetBrightness(); float x1 = colorArray[31].GetBrightness(); float x = colorArray[i].GetBrightness(); int rr = (int)Interpolate(shiftedColors[14].R, colorArray[31].R, x0, x1, x); int gg = (int)Interpolate(shiftedColors[14].G, colorArray[31].G, x0, x1, x); int bb = (int)Interpolate(shiftedColors[14].B, colorArray[31].B, x0, x1, x); ColorUtils.ValidateRGB(ref rr, ref gg, ref bb); shiftedColors[i] = Color.FromArgb(rr, gg, bb); } } } return(shiftedColors); }
private static Color HSL_StretchPixel(Color pixel, Dictionary <string, int[]> minMax) { return(HSL.ColorFromHSL((pixel.GetHue() / 360f), pixel.GetSaturation(), (((int)Math.Round(((pixel.GetBrightness() * 255) - minMax["Yellow"][0]) * (255.0f / (minMax["Yellow"][1] - minMax["Yellow"][0])))) / 255f))); }
/// <summary> /// Given a Color (RGB Struct) in range of 0-255 /// Return H,S,L in range of 0-1 /// </summary> /// <param name="rgb">Color object (RGB)</param> /// <param name="hue">Hue (0 - 360)</param> /// <param name="sl">Saturation (0 - 1)</param> /// <param name="l">Luminosity (0 - 1)</param> public static void RgbToHsl(Color rgb, out double h, out double s, out double l) { h = rgb.GetHue(); s = rgb.GetSaturation(); l = rgb.GetBrightness(); }
private static Bitmap _filteringHSV(Bitmap bmp, float[,] ope1, float[,] ope2 = null) { int xadj = -ope1.GetLength(0) / 2; int yadj = -ope1.GetLength(1) / 2; Bitmap dstBmp = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format8bppIndexed); var pal = dstBmp.Palette; for (int i = 0; i < 256; i++) { pal.Entries[i] = Color.FromArgb(i, i, i); } dstBmp.Palette = pal; BitmapEx src = BitmapEx.Begin(bmp, ImageLockMode.ReadOnly); BitmapEx dst = BitmapEx.Begin(dstBmp, ImageLockMode.WriteOnly); if (bmp.PixelFormat == PixelFormat.Format8bppIndexed) { throw new Exception("For RGB Bitmap only"); } else { int bmpWidth = bmp.Width; int bmpHeight = bmp.Height; System.Threading.Tasks.Parallel.For(0, bmpHeight, i => //for (int i = 0; i < bmp.Height; i++) { for (int j = 0; j < bmpWidth; j++) { float sum1H = 0, sum1S = 0, sum1V = 0; float sum2H = 0, sum2S = 0, sum2V = 0; for (int k = 0; k < ope1.GetLength(1); k++) { int y = i + k + yadj; for (int n = 0; n < ope1.GetLength(0); n++) { int x = j + n + xadj; if (x >= 0 && x < bmpWidth && y >= 0 && y < bmpHeight) { Color c = src.GetPixel(x, y); float h = c.GetHue() / 360.0F; //GetHue() 0.0-360.0 float s = c.GetSaturation(); //GetSaturation() 0.0-1.0 float v = c.GetBrightness(); //GetBrightness() 0.0-1.0 sum1H += h * ope1[n, k]; sum1S += s * ope1[n, k]; sum1V += v * ope1[n, k]; if (ope2 != null) { sum2H += h * ope2[n, k]; sum2S += s * ope2[n, k]; sum2V += v * ope2[n, k]; } } } } float sum; if (ope2 != null) { sum = (float)Math.Sqrt(sum1H * sum1H + sum1S * sum1S + sum1V * sum1V + sum2H * sum2H + sum2S * sum2S + sum2V * sum2V) * 255.0F / 6.0F; } else { sum = (float)Math.Sqrt(sum1H * sum1H + sum1S * sum1S + sum1V * sum1V) * 255.0F / 3.0F; } if (sum < 0.0F) { sum = 0.0F; } else if (sum > 255.0F) { sum = 255.0F; } dst.SetPixelDirect(j, i, (int)sum); } }); } src.End(); dst.End(); return(dstBmp); }
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(); }
public static Color addHue(Color color, int hue) { int a = color.A; float h = color.GetHue(); float s = color.GetSaturation(); float b = color.GetBrightness(); h += hue; if (h >= 360) { h -= 360; } float fMax, fMid, fMin; int iSextant, iMax, iMid, iMin; if (0.5 < b) { fMax = b - (b * s) + s; fMin = b + (b * s) - s; } else { fMax = b + (b * s); fMin = b - (b * s); } iSextant = (int)Math.Floor(h / 60f); if (300f <= h) { h -= 360f; } h /= 60f; h -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f); if (0 == iSextant % 2) { fMid = h * (fMax - fMin) + fMin; } else { fMid = fMin - h * (fMax - fMin); } iMax = Convert.ToInt32(fMax * 255); iMid = Convert.ToInt32(fMid * 255); iMin = Convert.ToInt32(fMin * 255); Color nColor; switch (iSextant) { case 1: nColor = Color.FromArgb(a, iMid, iMax, iMin); break; case 2: nColor = Color.FromArgb(a, iMin, iMax, iMid); break; case 3: nColor = Color.FromArgb(a, iMin, iMid, iMax); break; case 4: nColor = Color.FromArgb(a, iMid, iMin, iMax); break; case 5: nColor = Color.FromArgb(a, iMax, iMin, iMid); break; default: nColor = Color.FromArgb(a, iMax, iMid, iMin); break; } return(nColor); }
internal Dictionary <String, List <Color> > getColorsInImage(Bitmap imageAsBitmap) { int xCoord = 0, yCoord = 0; float temp = 0; this.imageWidth = imageAsBitmap.Width; this.imageHeight = imageAsBitmap.Height; for (xCoord = 0; xCoord < this.imageWidth; xCoord++) { for (yCoord = 0; yCoord < this.imageHeight; yCoord++) { Dictionary <float, String> imgVIBGYORHueDiffDict = new Dictionary <float, string>(); List <float> imgVIBGYORHueDiff = new List <float>(); Color imgPixelColor = imageAsBitmap.GetPixel(xCoord, yCoord); float pixelColorHue = imgPixelColor.GetHue(); temp = pixelColorHue - Color.Violet.GetHue(); temp = adjustHue(temp); imgVIBGYORHueDiffDict.Add(temp, "Violet"); temp = pixelColorHue - Color.Indigo.GetHue(); temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Indigo"); } temp = pixelColorHue - Color.Blue.GetHue(); temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Blue"); } temp = pixelColorHue - Color.Green.GetHue(); temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Green"); } temp = pixelColorHue - Color.Yellow.GetHue(); temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Yellow"); } temp = pixelColorHue - Color.Orange.GetHue(); temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Orange"); } temp = pixelColorHue - Color.Red.GetHue(); temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Red"); } //Color Red has 2 hues one is 0 degree and another is 360 degree. //C# GetHue() method gives only 0 degree. temp = pixelColorHue - 360; temp = adjustHue(temp); if (!imgVIBGYORHueDiffDict.ContainsKey(temp)) { imgVIBGYORHueDiffDict.Add(temp, "Red"); } foreach (float hueDiff in imgVIBGYORHueDiffDict.Keys) { imgVIBGYORHueDiff.Add(hueDiff); } float closestPixelColorByHue = imgVIBGYORHueDiff.Min(); if (_colorByPixel.ContainsKey(imgVIBGYORHueDiffDict[closestPixelColorByHue])) { _colorByPixel[imgVIBGYORHueDiffDict[closestPixelColorByHue]].Add(imgPixelColor); _colorKeyPixValue[imgVIBGYORHueDiffDict[closestPixelColorByHue]].Add(pixelColorHue); _brightnessColorDict[imgVIBGYORHueDiffDict[closestPixelColorByHue]] += imgPixelColor.GetBrightness(); } else { List <Color> pixelColorStructure = new List <Color>(); List <float> pixelsHues = new List <float>(); pixelColorStructure.Add(imgPixelColor); pixelsHues.Add(pixelColorHue); _colorByPixel.Add(imgVIBGYORHueDiffDict[closestPixelColorByHue], pixelColorStructure); _colorKeyPixValue.Add(imgVIBGYORHueDiffDict[closestPixelColorByHue], pixelsHues); _brightnessColorDict.Add(imgVIBGYORHueDiffDict[closestPixelColorByHue], imgPixelColor.GetBrightness()); } } } return(_colorByPixel); }
/// <summary> /// Weighed distance using hue, saturation and brightness. /// </summary> /// <param name="colors"></param> /// <param name="target"></param> /// <returns></returns> public static int ClosestColorHsb(List<Color> colors, Color target) { float hue1 = target.GetHue(); var num1 = ColorNum(target); var diffs = colors.Select(n => Math.Abs(ColorNum(n) - num1) + GetHueDistance(n.GetHue(), hue1)); var diffMin = diffs.Min(x => x); return diffs.ToList().FindIndex(n => n == diffMin); }
public IActionResult Index(string hexfirstcolor, string hexsecondcolor, int?steps) { string test = hexfirstcolor; string test2 = hexsecondcolor; test = test.Remove(0, 1); test2 = test2.Remove(0, 1); bool testifhex1 = System.Text.RegularExpressions.Regex.IsMatch(test, @"\A\b[0-9a-fA-F]+\b\Z"); bool testifhex2 = System.Text.RegularExpressions.Regex.IsMatch(test2, @"\A\b[0-9a-fA-F]+\b\Z"); if (steps == null || !ModelState.IsValid || hexfirstcolor[0] != '#' || hexfirstcolor[0] != '#' || testifhex1 == false || testifhex2 == false) //if int is null or invalid { ViewBag.HexValue = false; return(View()); } else { ViewBag.success = true; if (hexfirstcolor.Length != 7 || hexsecondcolor.Length != 7) { int x = hexfirstcolor.Length; int z = hexsecondcolor.Length; while (hexfirstcolor.Length < 7) { hexfirstcolor += hexfirstcolor[x - 1]; hexsecondcolor += hexsecondcolor[z - 1]; } } List <string> Hex = new List <string>(); Color firstcolor = ColorTranslator.FromHtml(hexfirstcolor); Color secondcolor = ColorTranslator.FromHtml(hexsecondcolor); double value = firstcolor.GetBrightness(); double saturation = firstcolor.GetSaturation(); double hue = firstcolor.GetHue(); double value2 = secondcolor.GetBrightness(); double saturation2 = secondcolor.GetSaturation(); double hu2 = secondcolor.GetHue(); ColorToHSV(firstcolor, out hue, out saturation, out value); ColorToHSV(secondcolor, out hu2, out saturation2, out value2); Color holder; double hueincrement = (hue - hu2) / Convert.ToDouble(steps); double valueincrement = (value - value2) / Convert.ToDouble(steps); double saturationincrement = (saturation - saturation2) / Convert.ToDouble(steps); Hex.Add(hexfirstcolor); if (hueincrement < 0) { hueincrement *= -1; } if (valueincrement < 0) { valueincrement *= -1; } if (saturationincrement < 0) { saturationincrement *= -1; } for (int i = 0; i < steps; i++) { if (hue == hu2) //they are both equal and we are done { } else if (hue > hu2) { hue -= hueincrement; } else { hue += hueincrement; } if (saturation == saturation2) { } else if (saturation > saturation2) { saturation -= saturationincrement; } else { saturation += saturationincrement; } if (value == value2) { } else if (value > value2) { value -= valueincrement; } else { value += valueincrement; } holder = ColorFromHSV(hue, saturation, value); Hex.Add(ColorTranslator.ToHtml(holder)); } ViewBag.hexlist = Hex; return(View()); } }
public static Matrix[] readpnggetMatrixHSB(String imgpath) { float R = 0, G = 0, B = 0; Matrix[] mx = new Matrix[3]; Bitmap image = new Bitmap(imgpath); int i, j; // float[,] GreyImage = new float[image.Width, image.Height]; //[Row,Column] for (i = 0; i < mx.Length; i++) { mx[i] = new Matrix(image.Width, image.Height); } BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unsafe { byte *imagePointer1 = (byte *)bitmapData1.Scan0; for (i = 0; i < bitmapData1.Height; i++) { for (j = 0; j < bitmapData1.Width; j++) { if ((int)imagePointer1[3] != 0) { Color c = System.Drawing.Color.FromArgb(imagePointer1[2], imagePointer1[1], imagePointer1[0]); // mx[0].values[j, i] = c.GetBrightness()+ c.GetHue()+ c.GetHue(); mx[0].values[j, i] = c.GetBrightness(); //B mx[1].values[j, i] = c.GetHue(); //G mx[2].values[j, i] = c.GetHue(); //R R += c.GetHue(); G += c.GetHue(); B += c.GetBrightness(); //mx[0].values[j, i] = (float)imagePointer1[0] ;//B //mx[1].values[j, i] = (float)imagePointer1[1] ;//G //mx[2].values[j, i] = (float)imagePointer1[2] ;//R // GreyImage[j, i, 2] = (int)imagePointer1[3];//A //4 bytes per pixel } imagePointer1 += 4; }//end for j //4 bytes per pixel imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4); } //end for i } //end unsafe float agvR = R / (image.Width * image.Height); float agvG = G / (image.Width * image.Height); float agvB = B / (image.Width * image.Height); for (i = 0; i < bitmapData1.Height; i++) { for (j = 0; j < bitmapData1.Width; j++) { mx[0].values[j, i] = (mx[0].values[j, i] - (agvB + agvG + agvR)) / 3;//B } } image.UnlockBits(bitmapData1); image.Dispose(); return(mx); }
/// <summary> /// Gets the colour hue. /// </summary> /// <param name="colour">The colour.</param> /// <returns></returns> public static string GetColourHue(Color colour) { return($"Hue: { colour.GetHue().ToString() }"); }
public string CreateBitmap(DUETContext db, Design design) { try { Inspiration aInspiration = Inspiration.GetWithID(db, this.InspirationId); string path = App.ROOT + aInspiration.Src; // Create a Bitmap object from a file. using (Bitmap bitmapInspire = new Bitmap(path)) { bitmapInspire.SetResolution(96.0F, 96.0F); Size viewstampsize = new Size((int)(Width), (int)(Height)); int inspirex = App.factor(X, this.InspirationWidth, bitmapInspire.Width); int inspirey = App.factor(Y, this.InspirationHeight, bitmapInspire.Height); int inspirestampwidth = App.factor((int)(Width), design.ViewWidth, design.Width); int inspirestampheight = App.factor((int)(Height), design.ViewWidth, design.Width); //make square Size inspirestampsize = new Size(inspirestampwidth, inspirestampheight); inspirex = (int)(inspirex - (inspirestampwidth / 2)); inspirey = (int)(inspirey - (inspirestampheight / 2)); //get bitmap from inspire if (inspirex < 0) { inspirex = 0; } if (inspirey < 0) { inspirey = 0; } if (inspirex > bitmapInspire.Width - inspirestampwidth) { inspirex = bitmapInspire.Width - inspirestampwidth; } if (inspirey > bitmapInspire.Height - inspirestampheight) { inspirey = bitmapInspire.Height - inspirestampheight; } Rectangle inspirestampRect = new Rectangle(inspirex, inspirey, (int)(inspirestampwidth), (int)(inspirestampheight)); using (var inspirestampBitmap = bitmapInspire.Clone(inspirestampRect, PixelFormat.Format32bppPArgb)){ byte[] inspireBytes = App.ImageToByte(inspirestampBitmap, inspirestampsize, ImageFormat.Bmp); //Set filter with mask image string maskpath = App.ROOT + "images/" + Shape; using (Bitmap stampshape = new Bitmap(maskpath)) { stampshape.SetResolution(96.0F, 96.0F); using (var mask = App.ResizeImage(stampshape, inspirestampwidth, inspirestampheight)) { byte[] maskBytes = App.ImageToByte(mask, inspirestampsize, ImageFormat.Bmp); Color bgcolor = Color.FromArgb(Red, Green, Blue); float hue = bgcolor.GetHue(); float saturation = bgcolor.GetSaturation(); float brightness = bgcolor.GetBrightness(); var sumcolor = bgcolor.R + bgcolor.G + bgcolor.B; int start = maskBytes.Length - (inspirestampwidth * inspirestampheight * 4); for (int i = start; i < maskBytes.Length; i += 4) { if (maskBytes[i + 3] == 255) { maskBytes[i] = 0; maskBytes[i + 1] = 0; maskBytes[i + 2] = 0; maskBytes[i + 3] = 255; } else { maskBytes[i] = 255; maskBytes[i + 1] = 255; maskBytes[i + 2] = 255; maskBytes[i + 3] = 0; } } //in een bitmap wordt de array met kleuren van achteren naar voren opgebouwd dus het is elke keer B,G,R,A if (Type == "copy") { for (var i = start; i < maskBytes.Length; i += 4) { if (maskBytes[i + 3] < 255) { //transparant inspireBytes[i] = 255; inspireBytes[i + 1] = 255; inspireBytes[i + 2] = 255; inspireBytes[i + 3] = 255; } } } else if (Type == "color") { for (var i = start; i < maskBytes.Length; i += 4) { //if (maskBytes[i] == 255 && maskBytes[i + 1] == 255 && maskBytes[i + 2] == 255 && maskBytes[i + 3] == 0){ if (maskBytes[i + 3] < 100) { //transparant inspireBytes[i] = 255; inspireBytes[i + 1] = 255; inspireBytes[i + 2] = 255; inspireBytes[i + 3] = 255; } else { //color vergelijk color met bgcolor; Color acolor = Color.FromArgb(inspireBytes[i + 2], inspireBytes[i + 1], inspireBytes[i]); if (Math.Abs(acolor.GetHue() - hue) < 30) { //dezelfde kleur => verander niet } else { //maak wit => wit wordt transparant inspireBytes[i] = 255; inspireBytes[i + 1] = 255; inspireBytes[i + 2] = 255; inspireBytes[i + 3] = 255; } } } } else if (Type == "lightness") { for (var i = start; i < maskBytes.Length; i += 4) { //if (maskBytes[i] == 255 && maskBytes[i +1] == 255 && maskBytes[i + 2] == 255 && maskBytes[i + 3] == 0) if (maskBytes[i + 3] < 100) { //transparant inspireBytes[i] = 255; inspireBytes[i + 1] = 255; inspireBytes[i + 2] = 255; inspireBytes[i + 3] = 255; } else { //lightness vergelijk sum met stamp.bgcolor sum; Color acolor = Color.FromArgb(inspireBytes[i + 2], inspireBytes[i + 1], inspireBytes[i]); if (Math.Abs(acolor.GetBrightness() - brightness) < 0.1) { //dezefde lightness => verander niets } else { inspireBytes[i] = 255; inspireBytes[i + 1] = 255; inspireBytes[i + 2] = 255; inspireBytes[i + 3] = 255; } } } } //coversie van bytes[] naar memorystream naar bitmap using (var ms = new MemoryStream(inspireBytes)) { Bitmap = new Bitmap(ms); Bitmap.MakeTransparent(Color.FromArgb(255, 255, 255)); Bitmap.SetResolution(96.0F, 96.0F); //now create client site image using (var bitmap1 = App.ResizeImage(Bitmap, this.Width, this.Height)){ bitmap1.MakeTransparent(Color.FromArgb(255, 255, 255)); Byte[] bytes = App.ImageToByte(bitmap1, viewstampsize, ImageFormat.Png); //conversie van bytes naar dataurl return(Convert.ToBase64String(bytes)); } } } } } } } catch (Exception exception) { return("Error in stamp class: " + exception.Message); } }
private static List <Color> GetMainColorsByHsb(IEnumerable <ColorCount> colors, int maxCount, float minHueDiff, float minSatDiff, float minBriDiff) { List <Color> results = new List <Color>(); long lastCount = -1; foreach (ColorCount colorCount in colors) { long count = colorCount.Count; Color color = colorCount.Color; float bri = color.GetBrightness(); float sat = color.GetSaturation(); float hue = color.GetHue(); bool uniqueColorFound = true; // want main colors ie dark brown, gold, silver, not 3 shades of brown // Make sure color isn't too similar to already-added colors foreach (Color colorOther in results) { float briOther = colorOther.GetBrightness(); float satOther = colorOther.GetSaturation(); float hueOther = colorOther.GetHue(); // hue is 360 degrees of color, to calculate hue difference // need to subtract 360 when either are out by 180 (i.e red is at 0 and 359, diff should be 1 etc) if (hue - hueOther > 180) { hue -= 360; } if (hueOther - hue > 180) { hueOther -= 360; } float briDiff = Math.Abs(bri - briOther); float satDiff = Math.Abs(sat - satOther); float hueDiff = Math.Abs(hue - hueOther); int matchHSB = 0; if (briDiff <= minBriDiff) { matchHSB++; } if (satDiff <= minSatDiff) { matchHSB++; } if (hueDiff <= minHueDiff) { matchHSB++; } if (satDiff != 1 && (briDiff <= minBriDiff || satDiff <= minSatDiff || hueDiff <= minHueDiff)) { uniqueColorFound = false; break; } } if (uniqueColorFound) // color differs by min ammount of HSL so add to response { results.Add(color); if (results.Count == maxCount) { break; } } lastCount = count; } Trace.WriteLine($"Colors Found: {results.Count}/{maxCount}"); return(results); }
static void Main(string[] args) { const bool shouldInvertBrightness = false; const bool shouldChangeColor = true; Image image = Image.FromFile(@"..\..\ascii-pineapple.jpg"); //Image image = Image.FromFile(@"..\..\zebra.jpg"); Size maxSize = new Size(210, 150); ColorBightness colorBightness = new ColorBightness(Lightness); double MAX_BRIGHTNESS = colorBightness(Color.FromArgb(255, 255, 255)); string asciiText = "`^\",:;Il!i~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"; double ASCII_LENGTH_BRIGHTNESS_FACTOR = (asciiText.Length - 1) / MAX_BRIGHTNESS; char[] ascii = asciiText.ToCharArray(); using (Bitmap bmp = new Bitmap(image, maxSize)) { Console.WriteLine("Width : " + bmp.Width); Console.WriteLine("Height : " + bmp.Height); for (int i = 0; i < bmp.Height; i++) { for (int j = 0; j < bmp.Width; j++) { Color color = bmp.GetPixel(j, i); int index; if (shouldInvertBrightness) { index = (int)(InvertBrightness(colorBightness(color), MAX_BRIGHTNESS) * ASCII_LENGTH_BRIGHTNESS_FACTOR); } else { index = (int)(colorBightness(color) * ASCII_LENGTH_BRIGHTNESS_FACTOR); } if (shouldChangeColor) { float hue = color.GetHue(); if (color.GetBrightness() > .7) { Console.ForegroundColor = ConsoleColor.White; } else if (color.GetBrightness() < .3) { Console.ForegroundColor = ConsoleColor.DarkBlue; } else if (hue < 15 || hue >= 345) { Console.ForegroundColor = ConsoleColor.Red; } else if (hue < 75) { Console.ForegroundColor = ConsoleColor.Yellow; } else if (hue < 140) { Console.ForegroundColor = ConsoleColor.Green; } else if (hue < 200) { Console.ForegroundColor = ConsoleColor.Cyan; } else if (hue < 260) { Console.ForegroundColor = ConsoleColor.Blue; } else if (hue < 330) { Console.ForegroundColor = ConsoleColor.Magenta; } else if (hue < 345) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.White; } } Console.Write(ascii[index] + "" + ascii[index] + "" + ascii[index]); } Console.WriteLine(); } } Console.ReadKey(); }
public void bitmapColorAssocator(Bitmap image1) { for (int y = 0; y < image1.Height; y++) { for (int x = 0; x < image1.Width; x++) { Color pixel = image1.GetPixel(x, y); int avgRGB = (pixel.R + pixel.G + pixel.B) / 3; if (pixel.Name == "ff000000" || pixel.R <= MyGlobalVars.blackThreashold && pixel.G <= MyGlobalVars.blackThreashold && pixel.B <= MyGlobalVars.blackThreashold && avgRGB < 64) { matrix[x, y] = "-1 "; } else if (pixel.Name == "ffffffff") { matrix[x, y] = "-2 "; }//check if the RGB values in the threashold else if (pixel.R >= pixel.G - MyGlobalVars.whiteThreashold && pixel.R <= pixel.G + MyGlobalVars.whiteThreashold && pixel.G >= pixel.B - MyGlobalVars.whiteThreashold && pixel.G <= pixel.B + MyGlobalVars.whiteThreashold && pixel.B >= pixel.R - MyGlobalVars.whiteThreashold && pixel.B <= pixel.R + MyGlobalVars.whiteThreashold) { if (avgRGB > 255) { matrix[x, y] = "-2 "; } else if (avgRGB > 224) { matrix[x, y] = "-9 "; } else if (avgRGB > 192) { matrix[x, y] = "-8 "; } else if (avgRGB > 160) { matrix[x, y] = "-7 "; } else if (avgRGB > 128) { matrix[x, y] = "-6 "; } else if (avgRGB > 96) { matrix[x, y] = "-5 "; } else if (avgRGB > 64) { matrix[x, y] = "-4 "; } else if (avgRGB > 32) { matrix[x, y] = "-3 "; } else if (avgRGB > 0) { matrix[x, y] = "-1 "; } } else { matrix[x, y] = (int)(pixel.GetHue() / 1.41176470588) + " "; } } } }
public void Enhance(Bitmap bitmap, double saturation, double contrast) { BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { int size = bitmap.Width * bitmap.Height; int *pixels = (int *)data.Scan0; for (int i = 0; i < size; i++) { //System.Windows.Forms.Application.DoEvents(); Color col = Color.FromArgb(pixels[i]); double R = col.R; double G = col.G; double B = col.B; if (saturation < 1.0) { Color aux = Program.HSL2RGB(col.GetHue() / 360.0, col.GetSaturation() * saturation, col.GetBrightness()); R = aux.R; G = aux.G; B = aux.B; } if (contrast != 0.0) { double c; if (contrast < -100) { c = 0.0; } else if (contrast > 100) { c = 2.0; } else { c = (100.0 + contrast) / 100.0; } c *= c; double red = R / 255.0; red -= 0.5; red *= c; red += 0.5; R = red * 255.0; double green = G / 255.0; green -= 0.5; green *= c; green += 0.5; G = green * 255.0; double blue = B / 255.0; blue -= 0.5; blue *= c; blue += 0.5; B = blue * 255.0; } if (R > 255.0) { R = 255.0; } else if (R < 0.0) { R = 0.0; } if (G > 255.0) { G = 255.0; } else if (G < 0.0) { G = 0.0; } if (B > 255.0) { B = 255.0; } else if (B < 0.0) { B = 0.0; } pixels[i] = Color.FromArgb((int)R, (int)G, (int)B).ToArgb(); } } bitmap.UnlockBits(data); }
protected override int Render(YGraphics g, int w, int h) { mainViewPort = new ViewPortSettings() { IRLx = 0, IRLy = 0, zoomx = 1.0, zoomy = 1.0, Lmargin = 0, Rmargin = 0, Tmargin = 0, Bmargin = 0, Capture = false }; g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; drawAnnotationPanels(g, _annotationPanels, w, h, false, ref mainViewPort); DrawPrameters p = ComputeDrawParameters(g, w, h, mainViewPort); if (_path == null) { double outterlength = (2 * p.outerRadius * Math.PI) * (p.angleEnd - p.angleStart) / (2 * Math.PI); int stepCount = (int)(outterlength / SegmentMaxLength); double stepsize = (p.angleEnd - p.angleStart) / stepCount; _path = new PointF[2 * (stepCount + 1)]; int n = 0; for (int i = 0; i <= stepCount; i++) { double a = p.angleStart + i * stepsize; _path[n++] = new PointF((Single)(p.xcenter + p.outerRadius * Math.Cos(a)), (Single)(p.ycenter - p.outerRadius * Math.Sin(a))); } for (int i = stepCount; i >= 0; i--) { double a = p.angleStart + i * stepsize; _path[n++] = new PointF((Single)(p.xcenter + p.innerRadius * Math.Cos(a)), (Single)(p.ycenter - p.innerRadius * Math.Sin(a))); } } if (_bgBrush == null) { _bgBrush = new LinearGradientBrush(new Point(0, (int)(p.ycenter - p.heightTop)), new Point(0, (int)(p.ycenter + p.heightBottom)), _backgroundColor1, _backgroundColor2); } if (_borderpen == null) { _borderpen = new Pen(_borderColor, (float)_borderThickness); _borderpen.LineJoin = LineJoin.Round; } g.FillPolygon(_bgBrush, _path); if (_shownValue != _value) { double step = _maxSpeed * (_max - _min) / 100; if (Math.Abs(_value - _shownValue) < step) { _shownValue = _value; } else if (_shownValue < _value) { _shownValue += step; } else { _shownValue -= step; } } double v = _shownValue; if (v >= _min) { if (v > _max) { v = _max; } double valueFactor = (v - _min) / (_max - min); double angleValue = p.angleStart + (p.angleEnd - p.angleStart) * valueFactor; double outterlength = (2 * p.outerRadius * Math.PI) * (angleValue - p.angleStart) / (2 * Math.PI); int stepCount = (int)(outterlength / SegmentMaxLength); double stepsize = (angleValue - p.angleStart) / stepCount; PointF[] pt = new PointF[2 * (stepCount + 1)]; int n = 0; for (int i = 0; i <= stepCount; i++) { double a = p.angleEnd - i * stepsize; pt[n++] = new PointF((Single)(p.xcenter + p.outerRadius * Math.Cos(a)), (Single)(p.ycenter - p.outerRadius * Math.Sin(a))); } for (int i = stepCount; i >= 0; i--) { double a = p.angleEnd - i * stepsize; pt[n++] = new PointF((Single)(p.xcenter + p.innerRadius * Math.Cos(a)), (Single)(p.ycenter - p.innerRadius * Math.Sin(a))); } Brush b; if (_color1 == _color2) { b = new SolidBrush(_color1); } else { int A1 = (_color1.ToArgb() >> 24) & 0xFF; double H1 = _color1.GetHue(); double S1 = _color1.GetSaturation(); double L1 = _color1.GetBrightness(); int A2 = (_color2.ToArgb() >> 24) & 0xFF; double H2 = _color2.GetHue(); double S2 = _color2.GetSaturation(); double L2 = _color2.GetBrightness(); int A = ((int)Math.Round(A1 + (double)(A2 - A1) * valueFactor)) & 0xff; double H; if (Math.Abs(H2 - H1) <= 180) { H = H1 + (double)(H2 - H1) * valueFactor; } else { H = H1 + 360 + (double)(H2 - H1 + 360) * valueFactor; if (H > 360) { H -= 360; } } double S = S1 + (double)(S2 - S1) * valueFactor; double L = L1 + (double)(L2 - L1) * valueFactor; b = new SolidBrush(Color.FromArgb(A1, Ycolor.hsl2rgb((int)((255 * H) / 360), (int)(255 * S), (int)(255 * L)))); } g.FillPolygon(b, pt); } if (_borderThickness > 0) { g.DrawPolygon(_borderpen, _path); } g.DrawString(lastDrawParameters.value, _font.fontObject, _font.brushObject, p.valueRectangle, p.valueFormat); if (_showMinMax) { //Pen pn = new Pen(Color.Red); //g.DrawRectangle(pn,lastDrawParameters.minValueRectangle); //g.DrawRectangle(pn, lastDrawParameters.maxValueRectangle); g.DrawString(lastDrawParameters.minValue, _minMaxFont.fontObject, _minMaxFont.brushObject, lastDrawParameters.minValueRectangle, lastDrawParameters.minValueFormat); g.DrawString(lastDrawParameters.maxValue, _minMaxFont.fontObject, _minMaxFont.brushObject, lastDrawParameters.maxValueRectangle, lastDrawParameters.maxValueFormat); } drawAnnotationPanels(g, _annotationPanels, w, h, true, ref mainViewPort); DrawMessagePanels(g, w, h); return(0); }
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(); }
/// <summary> /// Closest match for hues only. /// </summary> /// <param name="colors"></param> /// <param name="target"></param> /// <returns></returns> public static int ClosestColorHue(List<Color> colors, Color target) { var hue1 = target.GetHue(); var diffs = colors.Select(n => GetHueDistance(n.GetHue(), hue1)); var diffMin = diffs.Min(n => n); return diffs.ToList().FindIndex(n => n == diffMin); }
/// <summary> /// Creates a new color scheme /// </summary> /// <param name="name">Name of scheme</param> /// <param name="baseColor">Base color of scheme</param> public SchemeSingle(string name, Color baseColor) { Name = name; BaseColor = baseColor; BaseColorHue = BaseColor.GetHue(); }
// relativeColors: Should color variables use a standard HUE and saturation and lightness values? public void UpdateThemeFiles(bool relativeColors = false) { HashSet <string> customColors = this.Harvest(); string colorVariables = "\n:root {\n"; // create a variable for the HUE of the color to act as the foundation for every other color if (relativeColors) { colorVariables += string.Format("\t--branding-color-hue: {0}; /* The HUE of the tenant's branding color */\n", (int)_primaryColor.GetHue()); } int counter = 1; // counter to label variable names foreach (string hexString in customColors) { Color oldColor = ColorTranslator.FromHtml(hexString); HSLColor oldHslColor = new HSLColor(oldColor); HSLColor newHslColor = null; if (hexString == ColorToHex(_defaultPrimaryColor)) { // don't do any hue or saturation operations newHslColor = new HSLColor(_primaryColor); } else { newHslColor = new HSLColor(_primaryColor.GetHue(), oldHslColor.Saturation, oldHslColor.Luminosity); } // new variable for the current hex string string variableName = string.Format("--branding-color-{0}", counter); string variableReference = string.Format("var({0})", variableName); if (relativeColors) { string relativeColor = string.Format("hsl({0}, {1}%, {2}%)", "var(--branding-color-hue)", (int)(newHslColor.Saturation / HSLColor.SCALE * 100.0), (int)(newHslColor.Luminosity / HSLColor.SCALE * 100)); colorVariables += string.Format("\t{0}: {1};\n", variableName, relativeColor); } else { string newHexString = ColorToHex(newHslColor); colorVariables += string.Format("\t{0}: {1};\n", variableName, newHexString); } foreach (CSSFile file in _newFilesContents) { file.Contents = file.Contents.Replace(hexString, variableReference); } counter++; } colorVariables += "}\n\n"; // write to disk foreach (CSSFile file in _newFilesContents) { if (file.FileName == "metronic-customize.css") { file.Contents = colorVariables + file.Contents; // also write to the demo tenant.css file File.WriteAllText(_demoTenantFile, colorVariables); } File.WriteAllText(file.FilePath, file.Contents); } }
/// <summary> /// <see cref="HSL"/> from the RGB color. /// </summary> /// <param name="c">The color.</param> /// <returns></returns> public static HSL FromRGB(Color c) { return(new HSL(c.GetHue(), c.GetSaturation(), c.GetBrightness())); }
private static string GetColourDataExtra(string colourDescription, Color selectedColour) { return($"{ colourDescription } Colour\nARGB: ({ ConversionMethods.FormatColourARGBString(selectedColour) })\nRGB: ({ ConversionMethods.FormatColourRGBString(selectedColour) })\nHexadecimal: { ConversionMethods.FormatColourToHexadecimal(selectedColour) }\n\nHue: { selectedColour.GetHue() }\nSaturation: { selectedColour.GetSaturation() }\nBrightness: { selectedColour.GetBrightness() }"); }
private string classifyColor(BINS bins, int[] rgb) { Color c = Color.FromArgb(0, rgb[0], rgb[1], rgb[2]); float hue = c.GetHue(); float sat = c.GetSaturation(); float lgt = c.GetBrightness(); int clrNum = 3; int interLeavingDistanceForH = 0; int numOfHues = 0; String delim = "-"; if (lgt < 0.2) { return("Color" + delim + 0); } if (lgt > 0.9 && sat < .1) { return("Color" + delim + 1); } if (sat < 0.25) { return("Color" + delim + 2); } if (bins == BINS.BINS_15) { interLeavingDistanceForH = (int)360 / (15 - 3); numOfHues = 15 - 3; } else if (bins == BINS.BINS_27) { interLeavingDistanceForH = (int)360 / (27 - 3); numOfHues = 27 - 3; } else if (bins == BINS.BINS_48) { interLeavingDistanceForH = (int)360 / (48 - 3); numOfHues = 48 - 3; } int currHue = 0; //go through all the color thresholds for (int i = 0; i < numOfHues; i++) { currHue += interLeavingDistanceForH; //if hue is less than curr threshold if (hue < currHue) { return("Color" + delim + clrNum); } //if it wasn't less than anything else if (i == numOfHues - 1) { } clrNum++; } return("Color" + delim + 3); }
public HSLColor(Color color) { Hue = color.GetHue(); Saturation = color.GetSaturation(); Lightness = color.GetBrightness(); }