我的彩虹拾色器不能正确饱和,我错过了什么/做错了什么?

uFo*_*ll8 5 c# user-interface colors sfml

我正在尝试创建一个类似于MS Paint的拾色器.不幸的是,我无法弄清楚饱和算法.每当我尝试实现饱和度时,它都不会正确饱和.我不得不对算法中的饱和效应有所了解.

在此输入图像描述

这是我当前的算法创建的.无论何时我尝试在Y轴上执行饱和效果,它只会使第一行之后的所有内容完全变为红色或黑色.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML;
using SFML.Graphics;
using SFML.Window;

namespace Source
{
   public ColorWheel()
    {
        for (int y = 0; y < 255; y++)
        {
            for (int z = 0; z < 6; z++)
            {
                for (int x = 0; x < 255; x++)
                {
                    uint ux = (uint)x;
                    uint uy = (uint)y;
                    uint uz = (uint)z;
                    ux = ux + (uz * 255);
                    image.SetPixel(ux, uy, color);

                    //Red 255 - Green 0-254
                    if (z == 0)
                    {
                        color.G += 1;
                    }
                    //Green 255 - Red 255-0
                    else if (z == 1)
                    {
                        color.R -= 1;
                    }
                    //Green 255 - Blue 0-255
                    else if (z == 2)
                    {
                        color.B += 1;
                    }
                    //Blue 255 - Green 255-0
                    else if (z == 3)
                    {
                        color.G -= 1;

                    }
                    //Blue 255 - Red 0-255
                    else if (z == 4)
                    {
                        color.R += 1;
                    }
                    //Red 255 - Blue 255-0
                    else if (z == 5)
                    {
                        color.B -= 1;
                    }

        Texture texture = new Texture(image);
        sprite.Texture = texture;
    }

    public void Update(double dt)
    {
    }

    public void Render(RenderWindow rWindow)
    {
        rWindow.Draw(sprite);
    }

}
Run Code Online (Sandbox Code Playgroud)

}

Ste*_*lls 2

我发现这是一个非常有趣的挑战。我花了一段时间,但我想我已经明白了。棘手的部分显然是要认识到,在第 1 行上,颜色介于 255 和 0 之间,而每个连续行的颜色较少(因为它们慢慢地混合为白色)。

因此,在第 10 行,您有 255 个像素和 245 种颜色可用于填充它们。无论如何,这是代码:

for (int y = 0; y < 255; y++)
        {
            color = new Color((255), y, y);
            for (int z = 0; z < 6; z++)
            {
                for (int x = 0; x < 255; x++)
                {
                    float colorDif = (255 / ((float)255 - y));
                    uint ux = (uint)x;
                    uint uy = (uint)y;
                    uint uz = (uint)z;
                    ux = ux + (uz * 255);
                    image.SetPixel(ux, uy, color);

                    if (x >= lastX + colorDif)
                    {
                        //Red 255 - Green 0-254
                        if (z == 0)
                        {
                            if (color.G < (255))
                                color.G += 1;
                        }
                        //Green 255 - Red 255-0
                        else if (z == 1)
                        {
                            if (color.R > y)
                                color.R -= 1;
                        }
                        //Green 255 - Blue 0-255
                        else if (z == 2)
                        {
                            if (color.B < (255))
                                color.B += 1;
                        }
                        //Blue 255 - Green 255-0
                        else if (z == 3)
                        {
                            if (color.G > y)
                                color.G -= 1;
                        }
                        //Blue 255 - Red 0-255
                        else if (z == 4)
                        {
                            if (color.R < (255))
                                color.R += 1;
                        }
                        //Red 255 - Blue 255-0
                        else if (z == 5)
                        {
                            if (color.B > y)
                                color.B -= 1;
                        }
                        lastX += colorDif;
                    }
                }
                lastX = 0;
            }
        }
Run Code Online (Sandbox Code Playgroud)

它似乎对我来说效果很好,但如果有任何问题,请告诉我,我会再看一下。希望能帮助到你!

编辑:并进一步解释一下,以防万一。这样做的目的是使颜色从全彩变为白色(这就是改变饱和度的作用)。但以防万一这不是您的意思,还有另外两种变体也应该可以正常工作。

如果您希望全部混合为黑色,您只需更改从y到 的最小颜色和从到 的0最大颜色。255255-y

您还可以混合为灰色,在这种情况下,您将需要 max as 255-y、 min asycolorDif = (255 / ((float)255 - 2 * y));(注意2 *)。然而,对于灰色,你只能得到 127 条颜色线,而不是 255 条。如果这没有意义,我可以尝试进一步解释:P