public float this[int x, int y] { get { if (!_valueMapMask[x, y]) { var hsvPixel = _converter.ToHsv(_image[x, y]); var v = Math.Max(0, (hsvPixel.V - 0.21f)) / (1f - 0.21f); var color = GetColor(x, y); if (color == ChatColor.Unknown || color == ChatColor.Redtext) { v = 0; } else if (color == ChatColor.ChatTimestampName) { v = Math.Min(1f, (v / 0.8f)); //Timestamps and username max out at 0.8 } //else if (color == ChatColor.Redtext) // v += 0.3f; _valueMap[x, y] = v; _valueMapMask[x, y] = true; } return(_valueMap[x, y]); } }
/// <summary> /// Set Goron energy colors from the given options. /// </summary> /// <param name="options">Color options.</param> void SetGoronEnergyColors(GoronColorOptions options) { Colors.GoronPunchEnergyPrim = GetPunchPrim(options.Main); Colors.GoronPunchEnergyEnv1 = options.Extra1; Colors.GoronPunchEnergyEnv2 = options.Extra1; var inner = Mix(options.Main, Colors.GoronRollInnerEnergyEnv); Colors.GoronRollInnerEnergyEnv = inner.Item1; Colors.GoronRollInnerEnergyPrim = inner.Item2; var outer1 = Mix(options.Extra1, Colors.GoronRollOuterEnergyEnv1); Colors.GoronRollOuterEnergyEnv1 = outer1.Item1; Colors.GoronRollOuterEnergyPrim1 = outer1.Item2; var outer2 = Mix(options.Extra2, Colors.GoronRollOuterEnergyEnv2); Colors.GoronRollOuterEnergyEnv2 = outer2.Item1; Colors.GoronRollOuterEnergyPrim2 = outer2.Item2; Color GetPunchPrim(Color color) { var converter = new ColorSpaceConverter(); return(converter.TranslateHsv(color, hsv => new Hsv(hsv.H, (hsv.S / 5f) * 4f, hsv.V))); } (Color env, Color prim) Mix(Color color, Color env) { // Convert color values to HSV. var converter = new ColorSpaceConverter(); var curHsv = converter.ToHsv(color); var envHsv = converter.ToHsv(env); // Calculate delta of Value. var envHsvValDelta = ((curHsv.V + envHsv.V) / 2f) - curHsv.V; // Multiply by Saturation to limit deviation of Value for greyscale-approaching colors (Saturation approaching 0). var envHsvValue = options.MixEnvValue ? curHsv.V + (envHsvValDelta * curHsv.S) : curHsv.V; // Build new colors from HSV. var envResultHsv = new Hsv(curHsv.H, curHsv.S, envHsvValue); var primResultHsv = new Hsv(curHsv.H, curHsv.S, curHsv.V); // Convert back into RGB. var envResult = converter.ToColor(envResultHsv); var primResult = converter.ToColor(primResultHsv); return(envResult, primResult); } }
public void Convert_CieXyz_to_Hsv(float x, float y, float yl, float h, float s, float v) { // Arrange var input = new CieXyz(x, y, yl); var expected = new Hsv(h, s, v); Span <CieXyz> inputSpan = new CieXyz[5]; inputSpan.Fill(input); Span <Hsv> actualSpan = new Hsv[5]; // Act var actual = Converter.ToHsv(input); Converter.Convert(inputSpan, actualSpan); // Assert Assert.Equal(expected, actual, ColorSpaceComparer); for (int i = 0; i < actualSpan.Length; i++) { Assert.Equal(expected, actualSpan[i], ColorSpaceComparer); } }
public void Convert_CieLuv_to_Hsv(float l, float u, float v, float h, float s, float v2) { // Arrange var input = new CieLuv(l, u, v); var expected = new Hsv(h, s, v2); Span <CieLuv> inputSpan = new CieLuv[5]; inputSpan.Fill(input); Span <Hsv> actualSpan = new Hsv[5]; // Act var actual = Converter.ToHsv(input); Converter.Convert(inputSpan, actualSpan, actualSpan.Length); // Assert Assert.Equal(expected, actual, ColorSpaceComparer); for (int i = 0; i < actualSpan.Length; i++) { Assert.Equal(expected, actualSpan[i], ColorSpaceComparer); } }
public void Convert_Rgb_To_Hsv(float r, float g, float b, float h, float s, float v) { // Arrange var input = new Rgb(r, g, b); var expected = new Hsv(h, s, v); Span <Rgb> inputSpan = new Rgb[5]; inputSpan.Fill(input); Span <Hsv> actualSpan = new Hsv[5]; // Act var actual = Converter.ToHsv(input); Converter.Convert(inputSpan, actualSpan, actualSpan.Length); // Assert Assert.Equal(expected, actual, ColorSpaceComparer); for (int i = 0; i < actualSpan.Length; i++) { Assert.Equal(expected, actualSpan[i], ColorSpaceComparer); } }
/// <summary> /// Translate a given <see cref="Color"/> as <see cref="Hsv"/> values. /// </summary> /// <param name="converter">Converter.</param> /// <param name="color">Source <see cref="Color"/>.</param> /// <param name="func">Translate function.</param> /// <returns>Translated <see cref="Color"/>.</returns> public static Color TranslateHsv(this ColorSpaceConverter converter, Color color, Func <Hsv, Hsv> func) { var hsv = converter.ToHsv(ToRgb(color)); var adjustedHsv = func(hsv); return(ToColor(converter.ToRgb(adjustedHsv))); }
public void SaveGreyscaleImage(string imagePath, string outputPath, float minV, float maxV) { var converter = new ColorSpaceConverter(); using (Image <Rgba32> rgbImage = Image.Load(imagePath)) { for (int i = 0; i < rgbImage.Width * rgbImage.Height; i++) { var x = i % rgbImage.Width; var y = i / rgbImage.Width; var pixel = rgbImage[x, y]; var hsvPixel = converter.ToHsv(pixel); if (hsvPixel.V > minV && hsvPixel.V < maxV) { rgbImage[x, y] = Rgba32.Black; } else { rgbImage[x, y] = Rgba32.White; } } rgbImage.Save(outputPath); } }
public void Convert_Cmyk_to_Hsv(float c, float m, float y, float k, float h, float s, float v) { // Arrange var input = new Cmyk(c, m, y, k); var expected = new Hsv(h, s, v); Span <Cmyk> inputSpan = new Cmyk[5]; inputSpan.Fill(input); Span <Hsv> actualSpan = new Hsv[5]; // Act var actual = Converter.ToHsv(input); Converter.Convert(inputSpan, actualSpan, actualSpan.Length); // Assert Assert.Equal(expected, actual, ColorSpaceComparer); for (int i = 0; i < actualSpan.Length; i++) { Assert.Equal(expected, actualSpan[i], ColorSpaceComparer); } }
public void SaveChatColors(string imagePath, string outputPath) { var converter = new ColorSpaceConverter(); using (Image <Rgba32> rgbImage = Image.Load(imagePath)) { for (int i = 0; i < rgbImage.Width * rgbImage.Height; i++) { var x = i % rgbImage.Width; var y = i / rgbImage.Width; var pixel = rgbImage[x, y]; var hsvPixel = converter.ToHsv(pixel); var v = (hsvPixel.V - 0.21f) / (1f - 0.21f); //if (x == 369 && y == 1134) // System.Diagnostics.Debugger.Break(); if (IsChatColor(hsvPixel)) { rgbImage[x, y] = new Rgba32(v, v, v); } else { rgbImage[x, y] = Rgba32.Black; } } rgbImage.Save(outputPath); } }
public static Color GetComplementary(this Color src, bool preserveOpacity = false) { var c = src.ToPixel <Rgba32>(); var a = c.A; //if RGB values are close to each other by a diff less than 10%, then if RGB values are lighter side, //decrease the blue by 50% (eventually it will increase in conversion below), //if RBB values are on darker side, decrease yellow by about 50% (it will increase in conversion) var avgColorValue = (c.R + c.G + c.B) / 3.0; var dR = Math.Abs(c.R - avgColorValue); var dG = Math.Abs(c.G - avgColorValue); var dB = Math.Abs(c.B - avgColorValue); if (dR < 20 && dG < 20 && dB < 20) //The color is a shade of gray { if (avgColorValue < 123) //color is dark { c = new Rgba32(220, 230, 50, a); // #dce632 } else { c = new Rgba32(255, 255, 50, a); // #ffff32 } } if (!preserveOpacity) { a = Math.Max(a, (byte)127); //We don't want contrast color to be more than 50% transparent ever. } var hsb = colorSpaceConverter.ToHsv(new Rgb24(c.R, c.G, c.B)); var h = hsb.H; h = h < 180 ? h + 180 : h - 180; var r = colorSpaceConverter.ToRgb(new Hsv(h, hsb.S, hsb.V)); return(new Rgba32(r.R, r.G, r.B, a)); }
public static Color SaturateColor(Color color, float saturate) { var hsv = _colorConverter.ToHsv(color.ToPixel <Rgb24>()); var newhsv = new Hsv(hsv.H, Math.Min(1.0f, hsv.S * saturate), hsv.V); var newrgb = _colorConverter.ToRgb(newhsv); var r = (byte)Math.Clamp(newrgb.R * 255, 0, 255); var g = (byte)Math.Clamp(newrgb.G * 255, 0, 255); var b = (byte)Math.Clamp(newrgb.B * 255, 0, 255); return(Color.FromRgb(r, g, b)); }
public void Convert_Rgb_To_Hsv(float r, float g, float b, float h, float s, float v) { // Arrange var input = new Rgb(r, g, b); // Act Hsv output = Converter.ToHsv(input); // Assert Assert.Equal(h, output.H, FloatRoundingComparer); Assert.Equal(s, output.S, FloatRoundingComparer); Assert.Equal(v, output.V, FloatRoundingComparer); }
public float this[int x, int y] { get { if (!_valueMapMask[x, y]) { var hsvPixel = _converter.ToHsv(_image[x, y]); var v = Math.Max(0, (hsvPixel.V - 0.21f)) / (1f - 0.21f); var color = GetColor(x, y); if (color == ChatColor.Unknown) { v = 0; } else if (color == ChatColor.Redtext) { v += 0.3f; } _valueMap[x, y] = v; _valueMapMask[x, y] = true; } return(_valueMap[x, y]); } }
/// <summary> /// Converts the full color game window into a image of the chat window in grayscale. /// </summary> /// <param name="imagePath">The path to the game screenshot</param> /// <param name="outputDirectory">The directory to save the processed image</param> /// <returns>The full path to the processed image</returns> public string ProcessChatImage(string imagePath, string outputDirectory) { var converter = new ColorSpaceConverter(); var chatRect = new Rectangle(4, 893, 3236, 1350); //Image.Load(@"C:\Users\david\OneDrive\Documents\WFChatParser\Screenshot (7).png") //@"C:\Users\david\OneDrive\Documents\WFChatParser\friday_last_moved.png" using (Image <Rgba32> rgbImage = Image.Load(imagePath)) { if (rgbImage.Height == 1440) { chatRect = new Rectangle(4, 530, 2022, 850); } else if (rgbImage.Height == 1080) { chatRect = new Rectangle(4, 370, 1507, 650); } rgbImage.Mutate(x => x.Crop(chatRect)); var minV = 0.29; for (int i = 0; i < rgbImage.Width * rgbImage.Height; i++) { var x = i % rgbImage.Width; var y = i / rgbImage.Width; var pixel = rgbImage[x, y]; var hsvPixel = converter.ToHsv(pixel); if (hsvPixel.V > minV) { rgbImage[x, y] = Rgba32.Black; } else { rgbImage[x, y] = Rgba32.White; } } var file = new FileInfo(imagePath); var outputPath = Path.Combine(outputDirectory, file.Name); rgbImage.Save(outputPath); return(outputPath); } }
/// <summary> /// Convert <see cref="Color"/> to <see cref="Hsv"/>. /// </summary> /// <param name="converter">Converter.</param> /// <param name="color">Source <see cref="Color"/>.</param> /// <returns>Converted <see cref="Hsv"/>.</returns> public static Hsv ToHsv(this ColorSpaceConverter converter, Color color) { return(converter.ToHsv(ToRgb(color))); }