fix color conversions

This commit is contained in:
13xforever 2020-05-23 04:06:23 +05:00
parent cf396a9556
commit 2194c53052

View File

@ -1,6 +1,4 @@
using System; using System;
using CompatApiClient.Utils;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using SixLabors.ImageSharp.ColorSpaces; using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion; using SixLabors.ImageSharp.ColorSpaces.Conversion;
@ -13,13 +11,15 @@ namespace CompatBot.Utils.Extensions
private static readonly ColorSpaceConverter colorSpaceConverter = new ColorSpaceConverter(); private static readonly ColorSpaceConverter colorSpaceConverter = new ColorSpaceConverter();
public static Color ToStandardColor(this ColorThiefDotNet.Color c) public static Color ToStandardColor(this ColorThiefDotNet.Color c)
=> new Argb32(c.R, c.G, c.B, c.A); => new Rgba32(c.R, c.G, c.B, c.A);
public static Color GetComplementary(this Color src, bool preserveOpacity = false) public static Color GetComplementary(this Color src, bool preserveOpacity = false)
{ {
var c = src.ToPixel<Argb32>(); var c = src.ToPixel<Rgba32>();
var a = c.A; 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) //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 avgColorValue = (c.R + c.G + c.B) / 3.0;
var dR = Math.Abs(c.R - avgColorValue); var dR = Math.Abs(c.R - avgColorValue);
var dG = Math.Abs(c.G - avgColorValue); var dG = Math.Abs(c.G - avgColorValue);
@ -27,9 +27,9 @@ namespace CompatBot.Utils.Extensions
if (dR < 20 && dG < 20 && dB < 20) //The color is a shade of gray if (dR < 20 && dG < 20 && dB < 20) //The color is a shade of gray
{ {
if (avgColorValue < 123) //color is dark if (avgColorValue < 123) //color is dark
c = new Argb32(a, 220, 230, 50); c = new Rgba32(220, 230, 50, a);
else else
c = new Argb32(a, 255, 255, 50); c = new Rgba32(255, 255, 50, a);
} }
if (!preserveOpacity) if (!preserveOpacity)
a = Math.Max(a, (byte)127); //We don't want contrast color to be more than 50% transparent ever. a = Math.Max(a, (byte)127); //We don't want contrast color to be more than 50% transparent ever.
@ -37,7 +37,7 @@ namespace CompatBot.Utils.Extensions
var h = hsb.H; var h = hsb.H;
h = h < 180 ? h + 180 : h - 180; h = h < 180 ? h + 180 : h - 180;
var r = colorSpaceConverter.ToRgb(new Hsv(h, hsb.S, hsb.V)); var r = colorSpaceConverter.ToRgb(new Hsv(h, hsb.S, hsb.V));
return new Argb32(a, r.R, r.G, r.B); return new Rgba32(r.R, r.G, r.B, a);
} }
} }
} }