Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中以RGB创建类颜色#_C# - Fatal编程技术网

C# 在C中以RGB创建类颜色#

C# 在C中以RGB创建类颜色#,c#,C#,我尝试在C#中的RGB中创建类颜色,颜色值(红色、绿色、蓝色)必须在范围[0,1]内。 这是我的密码 public class ColorRGB { private ColorRGB(double _red, double _green, double _blue) { Red = _red; Green = _green; Blue = _blue; }

我尝试在C#中的RGB中创建类颜色,颜色值(红色、绿色、蓝色)必须在范围[0,1]内。
这是我的密码

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;
    }
}
公共类ColorRGB
{
专用颜色RGB(双红、双绿、双蓝)
{
红色=_红色;
绿色=_绿色;
蓝色=_蓝色;
}
公共静态颜色RGB ColorRGB_RGB模型(双红、双绿、双蓝)
{
返回新的颜色RGB(_红、_绿、_蓝);
}
公共静态颜色RGB ColorRGB_CMYModel(双青色、双品红、双黄)
{
var _red=1-_青色;
var _绿=1-_品红;
var_蓝色=1-_黄色;
返回新的颜色RGB(_红、_绿、_蓝);
}
公共颜色RGB AddRGB(颜色RGB\u第二颜色)
{
返回颜色rgb_rgb模型(this.Red+_secondColor.Red,this.Green+_secondColor.Green,this.Blue+_secondColor.Blue);
}
公共颜色RGB减去RGB(颜色RGB\u第二颜色)
{
返回颜色rgb_rgb模型(this.Red-_secondColor.Red,this.Green-_secondColor.Green,this.Blue-_secondColor.Blue);
}
公共双红{
获取{返回红色;}
私有集{red=red<0?0:(red>1?1:值);}
}
公共双绿
{
获取{返回绿色;}
私有集{green=green<0?0:(green>1?1:value);}
}
公共双蓝
{
获取{返回蓝色;}
私有集{blue=blue<0?0:(blue>1?1:值);}
}
公共双青色
{
获取{返回青色;}
私有集{cyan=cyan<0?0:(cyan>1?1:值);}
}
公共双品红色
{
获取{返回洋红色;}
私有集{magenta=magenta<0?0:(magenta>1?1:值);}
}
公共双黄
{
获取{返回黄色;}
私有集{yellow=yellow<0?0:(yellow>1?1:值);}
}
私人双红;
私人双绿;
私人双蓝;
私人双青色;
私人双品红色;
私人双黄;
}
}
但是当我试图创建这个类的实例并试图将颜色(红色、贪婪、蓝色)的值设置为超出范围[0,1]时,我的检查
red=red<0?0:(红色>1?1:数值)和其他代码无效。

有人能解释一下为什么会发生这种情况以及如何解决吗?

多用
value
少用
red

public double Red {
    get { return red; }
    private set { red = value < 0 ? 0 : (value > 1 ? 1 : value); }
}
然后将您的setters重新编写为:

private set { red = Bound(value); }

这将使您的代码看起来更干净

多使用
多使用
红色
少使用:

public double Red {
    get { return red; }
    private set { red = value < 0 ? 0 : (value > 1 ? 1 : value); }
}
然后将您的setters重新编写为:

private set { red = Bound(value); }

这将使您的代码看起来更干净

我打算建议将边界检查分解到它自己的函数中,以使测试更容易。。但答案是这样的:)我创建并使用了方法绑定,它是有效的,但reSharped建议我将此方法设置为静态的。为什么?@Heidel-因为该方法本身不访问类的任何状态(如局部变量)-它只对接收到的参数进行操作-所以它不需要是成员函数。我打算建议将边界检查分解到它自己的函数中,以使测试更容易。。但答案是这样的:)我创建并使用了方法绑定,它是有效的,但reSharped建议我将此方法设置为静态的。为什么?@Heidel-因为方法本身不访问类的任何状态(如局部变量)——它只对接收到的参数进行操作——所以它不需要是成员函数。