//internal static float[] ColorToHSV(int originalColor) //{ // float[] hsv = new float[3]; // SKColor.RGBToHSV(Color.GetRedComponent(originalColor), Color.GetGreenComponent(originalColor), Color.GetBlueComponent(originalColor), hsv); // return hsv; //} //public static int colorByChangingHue(int originalColor, float newHue) //{ // float[] hsv = ColorToHSV(originalColor); // hsv[0] = newHue; // return Color.HSVToColor(Color.GetAlphaComponent(originalColor), hsv); //} public static SKColor colorByChangingSaturation(SKColor originalColor, float newSaturation) { float h, s, v; originalColor.ToHsv(out h, out s, out v); return(SKColor.FromHsv(h, newSaturation, v)); }
private async Task GetColorAsync(SKColor color) { // Force alpha to 255. color = color.WithAlpha(0xFF); // Get raw uint32 value from color by extracting rgb values. uint raw = (uint)(color.Red << 16 | color.Green << 8 | color.Blue); // Get HSL and HSV values. var hsl = new Vector3(); var hsv = new Vector3(); color.ToHsl(out hsl.X, out hsl.Y, out hsl.Z); color.ToHsv(out hsv.X, out hsv.Y, out hsv.Z); // Draw color to image. var bitmap = new SKBitmap(128, 128); var surface = SKSurface.Create(bitmap.Info); using (var canvas = surface.Canvas) { canvas.Clear(color); canvas.Flush(); } // Encode image to stream for uploading. var stream = surface.Snapshot().Encode().AsStream(); // Use hex code as filename but omit the pound sign. string fileName = $"{raw.ToString("X6")}.png"; // Construct embed with color information and thumbnail. var embed = new EmbedBuilder() .WithThumbnailUrl($"attachment://{fileName}") .WithColor(raw) .AddField( // Strip alpha from hex. "HEX", $"`#{raw.ToString("X6")}`") .AddField( "RGB", $"`rgb({color.Red.ToString()}, {color.Green.ToString()}, {color.Blue.ToString()})`") .AddField( "HSL", $"**H**: {hsl.X.ToString("F2")}, **S**: {hsl.Y.ToString("F2")}, **L**: {hsl.Z.ToString("F2")}") .AddField( "HSV", $"**H**: {hsv.X.ToString("F2")}, **S**: {hsv.Y.ToString("F2")}, **V**: {hsv.Z.ToString("F2")}") .Build(); await Context.Interaction.RespondWithFileAsync(stream, fileName, embed : embed); }
public void ColorRgbToHsv() { var tuples = new List <ToOtherColor> { new ToOtherColor(new SKColor(000, 000, 000), new SKOtherColor(000f, 000.0f, 000.0f), "Black"), new ToOtherColor(new SKColor(255, 000, 000), new SKOtherColor(000f, 100.0f, 100.0f), "Red"), new ToOtherColor(new SKColor(255, 255, 000), new SKOtherColor(060f, 100.0f, 100.0f), "Yellow"), new ToOtherColor(new SKColor(255, 255, 255), new SKOtherColor(000f, 000.0f, 100.0f), "White"), new ToOtherColor(new SKColor(128, 128, 128), new SKOtherColor(000f, 000.0f, 050.2f), "Gray"), new ToOtherColor(new SKColor(128, 128, 000), new SKOtherColor(060f, 100.0f, 050.2f), "Olive"), new ToOtherColor(new SKColor(000, 128, 000), new SKOtherColor(120f, 100.0f, 050.2f), "Green"), new ToOtherColor(new SKColor(000, 000, 128), new SKOtherColor(240f, 100.0f, 050.2f), "Navy"), }; foreach (var item in tuples) { // values SKColor rgb = item.Item1; SKOtherColor other = item.Item2; // to HSV float h, s, v; rgb.ToHsv(out h, out s, out v); Assert.AreEqual(other.Item1, h, EPSILON, item.Item3 + " H"); Assert.AreEqual(other.Item2, s, EPSILON, item.Item3 + " S"); Assert.AreEqual(other.Item3, v, EPSILON, item.Item3 + " V"); // to RGB SKColor back = SKColor.FromHsv(other.Item1, other.Item2, other.Item3); Assert.AreEqual(rgb.Red, back.Red, item.Item3 + " R"); Assert.AreEqual(rgb.Green, back.Green, item.Item3 + " G"); Assert.AreEqual(rgb.Blue, back.Blue, item.Item3 + " B"); Assert.AreEqual(rgb.Alpha, back.Alpha, item.Item3 + " A"); } }