Beispiel #1
0
    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 OrbitingStarParticle(float x, float y, float orbit, Random randomizer, SKColor color) : base(x, y, 10, 10)
        {
            _centerX = x;
            _centerY = y;
            _radius  = randomizer.Next((int)(orbit), (int)(4 * orbit / 2));
            float depthZ = randomizer.Next(1, 7);

            _size        = (7 - depthZ);
            _shift       = (float)Math.PI * randomizer.Next(360) / 360;
            _speed       = (float)Math.PI * randomizer.Next(120, 360) / (360 * 50);
            _color       = color;
            _phaseSpeedX = 1;
            _phaseSpeedY = randomizer.Next(1, 3);


            float hHsl;
            float sHsl;
            float lHslBase;

            _color.ToHsl(out hHsl, out sHsl, out lHslBase);
            _lHslBase = lHslBase;
        }
Beispiel #3
0
        public void ColorRgbToHsl()
        {
            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, 050.0f), "Red"),
                new ToOtherColor(new SKColor(255, 255, 000), new SKOtherColor(060f, 100.0f, 050.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, 025.1f), "Olive"),
                new ToOtherColor(new SKColor(000, 128, 000), new SKOtherColor(120f, 100.0f, 025.1f), "Green"),
                new ToOtherColor(new SKColor(000, 000, 128), new SKOtherColor(240f, 100.0f, 025.1f), "Navy"),
            };

            foreach (var item in tuples)
            {
                // values
                SKColor      rgb   = item.Item1;
                SKOtherColor other = item.Item2;

                // to HSL
                float h, s, l;
                rgb.ToHsl(out h, out s, out l);

                Assert.AreEqual(other.Item1, h, EPSILON, item.Item3 + " H");
                Assert.AreEqual(other.Item2, s, EPSILON, item.Item3 + " S");
                Assert.AreEqual(other.Item3, l, EPSILON, item.Item3 + " L");

                // to RGB
                SKColor back = SKColor.FromHsl(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");
            }
        }