简体   繁体   English

在C#中以RGB创建类Color

[英]Create class Color in RGB in C#

I tried to create class Color in RGB in C#, values of colors (red, green, blue) must be inside range [0,1]. 我试图在C#中以RGB创建类Color,颜色(红色,绿色,蓝色)的值必须在[0,1]范围内。
It's my code 这是我的代码

public class ColorRGB
    {
        private ColorRGB(double _red, double _green, double _blue)
        {
            Red = _red;
            Green = _green;
            Blue = _blue;
        }

        public static ColorRGB ColorRGB_RGBModel(double _red, double _green, double _blue)
        {
            return new ColorRGB(_red, _green, _blue);
        }

        public static ColorRGB ColorRGB_CMYModel(double _cyan, double _magenta, double _yellow)
        {
            var _red = 1 - _cyan;
            var _green = 1 - _magenta;
            var _blue = 1 - _yellow;
            return new ColorRGB(_red, _green, _blue);
        }

        public ColorRGB AddRGB(ColorRGB _secondColor)
        {
            return ColorRGB_RGBModel(this.Red + _secondColor.Red, this.Green + _secondColor.Green, this.Blue + _secondColor.Blue);
        }

        public ColorRGB SubtractRGB(ColorRGB _secondColor)
        {
            return ColorRGB_RGBModel(this.Red - _secondColor.Red, this.Green - _secondColor.Green, this.Blue - _secondColor.Blue);
        }

        public double Red {
            get { return red; }
            private set { red = red < 0 ? 0 : (red > 1 ? 1 : value); }
        }
        public double Green
        {
            get { return green; }
            private set { green = green < 0 ? 0 : (green > 1 ? 1 : value); }
        }
        public double Blue
        {
            get { return blue; }
            private set { blue = blue < 0 ? 0 : (blue > 1 ? 1 : value); }
        }
        public double Cyan
        {
            get { return cyan; }
            private set { cyan = cyan < 0 ? 0 : (cyan > 1 ? 1 : value); }
        }
        public double Magenta
        {
            get { return magenta; }
            private set { magenta = magenta < 0 ? 0 : (magenta > 1 ? 1 : value); }
        }
        public double Yellow
        {
            get { return yellow; }
            private set { yellow = yellow < 0 ? 0 : (yellow > 1 ? 1 : value); }
        }

        private double red;
        private double green;
        private double blue;
        private double cyan;
        private double magenta;
        private double yellow;
    }
}

but when I tried to create an instance of this class and tried to set values of colors (red, greed, blue) out of range [0, 1], my checkings red = red < 0 ? 0 : (red > 1 ? 1 : value); 但是当我尝试创建此类的实例并尝试设置颜色值(红色,贪婪,蓝色)超出[0,1]范围时,我的检查red = red < 0 ? 0 : (red > 1 ? 1 : value); red = red < 0 ? 0 : (red > 1 ? 1 : value); and others didn't work. 和其他人没有工作。
Can someone explain me why it happens and how to fix that? 有人可以解释一下为什么会发生这种情况以及如何解决该问题吗?

Use value more and red less: value增加而red减少:

public double Red {
    get { return red; }
    private set { red = value < 0 ? 0 : (value > 1 ? 1 : value); }
}

At the moment, you're checking the current value of the local variable, rather than the value that the user is trying to set. 目前,您正在检查局部变量的当前值,而不是用户尝试设置的值。 (Similarly, for all of the other set methods - you're checking the local variable, which contains the old value that's about to be replaced - not the value parameter) (类似地,对于所有其他set方法-您正在检查局部变量,该变量包含将要替换的旧值-而不是value参数)


I'm also unsure why you have CMY properties/variables when they're ignored by your constructor, and never relate to the RGB values - but maybe that's code that you're yet to write. 我也不确定为什么构造函数将它们忽略时却具有CMY属性/变量,而它们却从未与RGB值相关-但这也许就是您尚未编写的代码。


As suggested to Izzy, you could add a function to your method: 根据Izzy的建议,您可以在方法中添加一个函数:

private static double Bound(double value) {
   return value < 0 ? 0 : (value > 1 ? 1 : value);
}

And then re-write your setters as: 然后将您的二传手改写为:

private set { red = Bound(value); }

Which should make you code look cleaner 哪个应该使您的代码看起来更简洁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM