Exemple #1
0
        private void ComputeNativeValues(int numChannels)
        {
            this.nativeColorValue = new float[numChannels];
            if (this.nativeColorValue.GetLength(0) > 0)
            {
                float[] sRGBValue = new float[3];

                sRGBValue[0] = this.sRgbColor.r / 255.0f;
                sRGBValue[1] = this.sRgbColor.g / 255.0f;
                sRGBValue[2] = this.sRgbColor.b / 255.0f;

                ColorTransform colorTransform = new ColorTransform(this.context, new ColorContext(PixelFormats.Bgra32));

                colorTransform.Translate(sRGBValue, this.nativeColorValue);
            }
        }
Exemple #2
0
        ///<summary>
        /// private helper function to set context values from a color value with a set context and ScRgb values
        ///</summary>
        ///
        private void ComputeScRgbValues()
        {
            if (this.context != null)
            {
                Color c2 = Color.FromRgb(0, 0, 0);

                c2.context = new ColorContext(PixelFormats.Bgra32);

                ColorTransform colorTransform = new ColorTransform(this.context, c2.context);
                float[]        scRGBValue     = new float[3];

                colorTransform.Translate(this.nativeColorValue, scRGBValue);

                this.scRgbColor.r = sRgbToScRgb((byte)((255.0f * scRGBValue[0]) + 0.5f));
                this.scRgbColor.g = sRgbToScRgb((byte)((255.0f * scRGBValue[1]) + 0.5f));
                this.scRgbColor.b = sRgbToScRgb((byte)((255.0f * scRGBValue[2]) + 0.5f));
            }
        }
Exemple #3
0
        /// <summary>
        /// Subtract operator - substracts each channel of the second color from each channel of the
        /// first and returns the result
        /// </summary>
        /// <param name='color1'>The minuend</param>
        /// <param name='color2'>The subtrahend</param>
        /// <returns>Returns the unclamped differnce</returns>
        public static Color operator -(Color color1, Color color2)
        {
            if (color1.context == null && color2.context == null)
            {
                Color c1 = FromScRgb(
                    color1.scRgbColor.a - color2.scRgbColor.a,
                    color1.scRgbColor.r - color2.scRgbColor.r,
                    color1.scRgbColor.g - color2.scRgbColor.g,
                    color1.scRgbColor.b - color2.scRgbColor.b
                    );
                return(c1);
            }
            else if (color1.context == null || color2.context == null)
            {
                throw new ArgumentException(SR.Get(SRID.Color_ColorContextTypeMismatch, null));
            }
            else if (color1.context == color2.context)
            {
                Color c1 = new Color();
                c1.context = color1.context;

                #pragma warning suppress 6506 // c1.context is obviously not null - both color1.context AND color2.context are not null
                c1.nativeColorValue = new float[c1.context.NumChannels];
                for (int i = 0; i < c1.nativeColorValue.GetLength(0); i++)
                {
                    c1.nativeColorValue[i] = color1.nativeColorValue[i] - color2.nativeColorValue[i];
                }

                Color c2 = Color.FromRgb(0, 0, 0);

                c2.context = new ColorContext(PixelFormats.Bgra32);

                ColorTransform colorTransform = new ColorTransform(c1.context, c2.context);
                float[]        sRGBValue      = new float[3];

                colorTransform.Translate(c1.nativeColorValue, sRGBValue);

                if (sRGBValue[0] < 0.0f)
                {
                    c1.sRgbColor.r = 0;
                }
                else if (sRGBValue[0] > 1.0f)
                {
                    c1.sRgbColor.r = 255;
                }
                else
                {
                    c1.sRgbColor.r = (byte)((sRGBValue[0] * 255.0f) + 0.5f);
                }

                if (sRGBValue[1] < 0.0f)
                {
                    c1.sRgbColor.g = 0;
                }
                else if (sRGBValue[1] > 1.0f)
                {
                    c1.sRgbColor.g = 255;
                }
                else
                {
                    c1.sRgbColor.g = (byte)((sRGBValue[1] * 255.0f) + 0.5f);
                }

                if (sRGBValue[2] < 0.0f)
                {
                    c1.sRgbColor.b = 0;
                }
                else if (sRGBValue[2] > 1.0f)
                {
                    c1.sRgbColor.b = 255;
                }
                else
                {
                    c1.sRgbColor.b = (byte)((sRGBValue[2] * 255.0f) + 0.5f);
                }

                c1.scRgbColor.r = sRgbToScRgb(c1.sRgbColor.r);
                c1.scRgbColor.g = sRgbToScRgb(c1.sRgbColor.g);
                c1.scRgbColor.b = sRgbToScRgb(c1.sRgbColor.b);
                c1.scRgbColor.a = color1.scRgbColor.a - color2.scRgbColor.a;
                if (c1.scRgbColor.a < 0.0f)
                {
                    c1.scRgbColor.a = 0.0f;
                    c1.sRgbColor.a  = 0;
                }
                else if (c1.scRgbColor.a > 1.0f)
                {
                    c1.scRgbColor.a = 1.0f;
                    c1.sRgbColor.a  = 255;
                }
                else
                {
                    c1.sRgbColor.a = (byte)((c1.scRgbColor.a * 255.0f) + 0.5f);
                }

                return(c1);
            }
            else
            {
                throw new ArgumentException(SR.Get(SRID.Color_ColorContextTypeMismatch, null));
            }
        }