Exemplo n.º 1
0
        public RGBColor(double alpha, double r, double g, double b, RGBColorSpace colorSpace)
            : base(colorSpace)
        {
            this.Alpha = new ColorChannel(colorSpace.Alpha, alpha);
            this.R = new ColorChannel(colorSpace.R, r);
            this.G = new ColorChannel(colorSpace.G, g);
            this.B = new ColorChannel(colorSpace.B, b);

            this.Channels = new ColorChannelCollection(Alpha, R, G, B);
        }
Exemplo n.º 2
0
        internal XYZColor(XYZColorSpace colorSpace, double alpha, double x, double y, double z)
            : base(colorSpace)
        {
            this.Alpha = new ColorChannel(colorSpace.Alpha, alpha);
            this.X = new ColorChannel(colorSpace.X, x);
            this.Y = new ColorChannel(colorSpace.Y, y);
            this.Z = new ColorChannel(colorSpace.Z, z);

            this.Channels = new ColorChannelCollection(Alpha, X, Y, Z);
        }
Exemplo n.º 3
0
        public xyYColor(Double alpha, double x, double y, double Y)
            : base(KnownColorSpaces.xyY)
        {
            this.Alpha = new ColorChannel(KnownColorSpaces.xyY.Alpha, alpha);
            this.x = new ColorChannel(KnownColorSpaces.xyY.x, x);
            this.y = new ColorChannel(KnownColorSpaces.xyY.y, y);
            this.Y = new ColorChannel(KnownColorSpaces.xyY.Y, Y);

            this.Channels = new ColorChannelCollection(Alpha, this.x, this.y, this.Y);
        }
Exemplo n.º 4
0
        public ColorHistogramDescriptor GetChannel(ColorChannel colorChannel)
        {
            var result = new ColorHistogramDescriptor
            {
                ColorChannel = colorChannel,
                Distribution = this.GetChannelDistribution((int)colorChannel)
            };

            return result;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get given color channel as a grayscale image.
        /// </summary>
        /// <param name="img">image</param>
        /// <param name="channel">color channel (r, g, b)</param>
        /// <returns>grayscale image</returns>
        public static sitk.Image GetColorChannelAsImage(sitk.Image img, ColorChannel channel)
        {
            sitk.Image result = null;
            sitk.VectorIndexSelectionCastImageFilter rgbVector = new sitk.VectorIndexSelectionCastImageFilter();

            switch (channel)
            {
            case ColorChannel.R:
                result = rgbVector.Execute(img, 0, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.G:
                result = rgbVector.Execute(img, 1, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.B:
                result = rgbVector.Execute(img, 2, sitk.PixelIDValueEnum.sitkFloat32); break;
            }

            return(result == null? img : result);
        }
Exemplo n.º 6
0
		public static Color AffectColorChannel (ColorChannel channel, Color originalColor, float value)
		{
			switch (channel)
			{
			case ColorChannel.RED:
				originalColor.r = value;
				break;
			case ColorChannel.GREEN:
				originalColor.g = value;
				break;
			case ColorChannel.BLUE:
				originalColor.b = value;
				break;
			case ColorChannel.ALPHA:
				originalColor.a = value;
				break;
			}
			return originalColor;
		}
Exemplo n.º 7
0
        public LabColor(double alpha, double L, double a, double b)
            : base(KnownColorSpaces.Lab)
        {
            this.Alpha = new ColorChannel(KnownColorSpaces.Lab.Alpha, alpha);
            this.a = new ColorChannel(KnownColorSpaces.Lab.a, a);
            this.b = new ColorChannel(KnownColorSpaces.Lab.b, b);

            // NOTE:    L should be in inclusive range of [0,100]
            //          some calculations may push it a little bit over 100 due to rounding errors
            //          in this implementation we will cut it at 100 exactly if that is a case
            if(L.IsGreaterThan(100.00))
            {
                L = 100.00;
            }

            this.L = new ColorChannel(KnownColorSpaces.Lab.Lightness, L);

            this.Channels = new ColorChannelCollection(Alpha, this.L, this.a, this.b);
        }
Exemplo n.º 8
0
    private static Vector4 ChannelToVector(ColorChannel channel)
    {
        switch (channel)
        {
        case ColorChannel.Red:
            return(new Vector4(1, 0, 0, 0));

        case ColorChannel.Green:
            return(new Vector4(0, 1, 0, 0));

        case ColorChannel.Blue:
            return(new Vector4(0, 0, 1, 0));

        case ColorChannel.Alpha:
            return(new Vector4(0, 0, 0, 1));

        default:
            return(new Vector4(0, 0, 0, 0));
        }
    }
Exemplo n.º 9
0
    Color GenerateBrightColor()
    {
        Color c = new Color();
        List <ColorChannel> channels = new List <ColorChannel> ();

        channels.Add(ColorChannel.r);
        channels.Add(ColorChannel.g);
        channels.Add(ColorChannel.b);

        //Select random primary color channel (R || G || B)
        int          p       = Random.Range(0, channels.Count);
        ColorChannel primary = channels [p];

        channels.RemoveAt(p);

        //Select random secondary color channel (R || G || B)
        int          s         = Random.Range(0, channels.Count);
        ColorChannel secondary = channels [s];

        float val  = 1;
        float val2 = Random.value;

        switch (primary)
        {
        case ColorChannel.r: c.r = val; break;

        case ColorChannel.g: c.g = val; break;

        case ColorChannel.b: c.b = val; break;
        }
        switch (secondary)
        {
        case ColorChannel.r: c.r = val2; break;

        case ColorChannel.g: c.g = val2; break;

        case ColorChannel.b: c.b = val2; break;
        }

        return(c);
    }
Exemplo n.º 10
0
 private void GetChannelImage(ImageData data, ColorChannel cc)
 {
     for (int i = 0; i < data.RGBValues.Length; i += 3)
     {
         if (cc == ColorChannel.Red)
         {
             data.RGBValues[i]     = 0;
             data.RGBValues[i + 1] = 0;
         }
         else if (cc == ColorChannel.Green)
         {
             data.RGBValues[i]     = 0;
             data.RGBValues[i + 2] = 0;
         }
         else if (cc == ColorChannel.Blue)
         {
             data.RGBValues[i + 1] = 0;
             data.RGBValues[i + 2] = 0;
         }
     }
 }
Exemplo n.º 11
0
        public static Color AffectColorChannel(ColorChannel channel, Color originalColor, float value)
        {
            switch (channel)
            {
            case ColorChannel.RED:
                originalColor.r = value;
                break;

            case ColorChannel.GREEN:
                originalColor.g = value;
                break;

            case ColorChannel.BLUE:
                originalColor.b = value;
                break;

            case ColorChannel.ALPHA:
                originalColor.a = value;
                break;
            }
            return(originalColor);
        }
Exemplo n.º 12
0
 public static float GetValue(Color c, ColorChannel channel)
 {
     if (channel == ColorChannel.Red)
     {
         return(c.R / 255f);
     }
     else if (channel == ColorChannel.Green)
     {
         return(c.G / 255f);
     }
     else if (channel == ColorChannel.Blue)
     {
         return(c.B / 255f);
     }
     else if (channel == ColorChannel.Alpha)
     {
         return(c.A / 255f);
     }
     else
     {
         return(c.GetBrightness());
     }
 }
Exemplo n.º 13
0
 public static byte GetValueRaw(Color c, ColorChannel channel)
 {
     if (channel == ColorChannel.Red)
     {
         return(c.R);
     }
     else if (channel == ColorChannel.Green)
     {
         return(c.G);
     }
     else if (channel == ColorChannel.Blue)
     {
         return(c.B);
     }
     else if (channel == ColorChannel.Alpha)
     {
         return(c.A);
     }
     else
     {
         return((byte)Math.Round(c.GetBrightness() * 255));
     }
 }
Exemplo n.º 14
0
        public static Color ChannelOffseted(this Color col, ColorChannel channel, float offset)
        {
            if (channel.HasFlag(ColorChannel.R))
            {
                col.r += offset;
            }

            if (channel.HasFlag(ColorChannel.G))
            {
                col.g += offset;
            }

            if (channel.HasFlag(ColorChannel.B))
            {
                col.b += offset;
            }

            if (channel.HasFlag(ColorChannel.A))
            {
                col.a += offset;
            }
            return(col);
        }
Exemplo n.º 15
0
 public static RGBImageBuilder From(ColorChannel red, ColorChannel green, ColorChannel blue)
 {
     return(new RGBImageBuilder(red));
 }
        /// <summary>
        /// Constructs a quantum circuit representing an image for a specific color channel.
        /// </summary>
        /// <param name="inputTexture">The texture from which a circuit is constructed</param>
        /// <param name="useLog">If logarithmic encoding should be used for the picture</param>
        /// <param name="colorChannel">The color channel (of the texture) which will be used to generate the image</param>
        /// <returns></returns>
        public QuantumCircuit GetCircuit(Texture2D inputTexture, bool useLog = false, ColorChannel colorChannel = ColorChannel.R)
        {
            //TODO optimize to get 3 channels directly
            double[,] imageData;

            switch (colorChannel)
            {
            case ColorChannel.R:
                imageData = QuantumImageHelper.GetRedHeightArray(inputTexture);
                break;

            case ColorChannel.G:
                imageData = QuantumImageHelper.GetGreenHeightArray(inputTexture);
                break;

            case ColorChannel.B:
                imageData = QuantumImageHelper.GetBlueHeightArray(inputTexture);
                break;

            case ColorChannel.A:
                imageData = QuantumImageHelper.GetAlphaHeightArray(inputTexture);
                break;

            default:
                imageData = QuantumImageHelper.GetGreyHeighArray(inputTexture);
                break;
            }

            return(GetCircuit(imageData, useLog));
        }
        /// <summary>
        /// Reads an ITK image as a grayscale image.
        /// </summary>
        /// <param name="file">filename</param>
        /// <param name="outputType">output datatype</param>
        /// <param name="channel">channel to extract</param>
        /// <returns>grayscale ITK image</returns>
        public static sitk.Image ReadITKImageAsGrayscaleFromFile(string file, sitk.PixelIDValueEnum outputType, ColorChannel channel)
        {
            sitk.ImageFileReader reader = new sitk.ImageFileReader();
            reader.SetFileName(file);
            reader.SetOutputPixelType(outputType);
            sitk.Image temp = reader.Execute();

            sitk.Image result = null;
            sitk.VectorIndexSelectionCastImageFilter rgbVector = new sitk.VectorIndexSelectionCastImageFilter();
            switch (channel)
            {
            case ColorChannel.R: result = rgbVector.Execute(temp, 0, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.G: result = rgbVector.Execute(temp, 1, sitk.PixelIDValueEnum.sitkFloat32); break;

            case ColorChannel.B: result = rgbVector.Execute(temp, 2, sitk.PixelIDValueEnum.sitkFloat32); break;
            }

            return(result);
        }
 public FullImageSeparatedTask(ColorChannel channel)
 {
     _channel = channel;
 }
Exemplo n.º 19
0
        private static double ItemCalculation(Color[][] itemMatrix, double[][] mask, int treshe, ColorChannel channel)
        {
            treshe -= 10;

            switch ((int)channel)
            {
            //blue
            case 0:
            {
                var res = 0.0;
                for (int i = 0; i < mask.Length; i++)
                {
                    for (int j = 0; j < mask.Length; j++)
                    {
                        res += ((double)itemMatrix[i][j].B) * mask[j][i];
                    }
                }
                return(res > treshe ? 255 : 0);
            }

            // green
            case 1:
            {
                var res = 0.0;
                for (int i = 0; i < mask.Length; i++)
                {
                    for (int j = 0; j < mask.Length; j++)
                    {
                        res += ((double)itemMatrix[i][j].G) * mask[j][i];
                    }
                }
                return(res > treshe ? 255 : 0);
            }

            // red
            case 2:
            {
                var res = 0.0;
                for (int i = 0; i < mask.Length; i++)
                {
                    for (int j = 0; j < mask.Length; j++)
                    {
                        res += itemMatrix[i][j].R * mask[j][i];
                    }
                }
                return(res > treshe ? 255 : 0);
            }

            default:
            {
                return(0);
            }
            }
        }
Exemplo n.º 20
0
 public static string GetAsShaderString(ColorChannel channel)
 {
     return(channel.ToString().ToLowerInvariant());
 }
Exemplo n.º 21
0
        public static double FillPartialHeightArray(Texture2D tex, double[,] imageData, ColorChannel channel = ColorChannel.R, int startWidth = 0, int startHeight = 0, int totalWidth = 8, int totalHeight = 8)
        {
            double max   = 0;
            double value = 0;

            switch (channel)
            {
            case ColorChannel.R:
                for (int x = 0; x < totalWidth; x++)
                {
                    for (int y = 0; y < totalHeight; y++)
                    {
                        value           = tex.GetPixel(x + startWidth, y + startHeight).r + MathHelper.Eps;
                        imageData[x, y] = value;
                        if (value > max)
                        {
                            max = value;
                        }
                    }
                }
                break;

            case ColorChannel.G:
                for (int x = 0; x < totalWidth; x++)
                {
                    for (int y = 0; y < totalHeight; y++)
                    {
                        value           = tex.GetPixel(x + startWidth, y + startHeight).g + MathHelper.Eps;
                        imageData[x, y] = value;
                        if (value > max)
                        {
                            max = value;
                        }
                    }
                }
                break;

            case ColorChannel.B:
                for (int x = 0; x < totalWidth; x++)
                {
                    for (int y = 0; y < totalHeight; y++)
                    {
                        value           = tex.GetPixel(x + startWidth, y + startHeight).b + MathHelper.Eps;
                        imageData[x, y] = value;
                        if (value > max)
                        {
                            max = value;
                        }
                    }
                }
                break;

            case ColorChannel.A:
                for (int x = 0; x < totalWidth; x++)
                {
                    for (int y = 0; y < totalHeight; y++)
                    {
                        value           = tex.GetPixel(x + startWidth, y + startHeight).a + MathHelper.Eps;
                        imageData[x, y] = value;
                        if (value > max)
                        {
                            max = value;
                        }
                    }
                }
                break;

            default:
                break;
            }
            return(max);
        }
Exemplo n.º 22
0
	public void CreateColorChannel()
	{
		colorChannel = new ColorChannel(width, height, partitionSizeX, partitionSizeY);
		colorChannel.Create();
	}
Exemplo n.º 23
0
        public void Draw(Rect rect, MeshSource meshSource, List <Vector2> uvBuffer, EditorWindow host)
        {
            Rect leftBorder = rect;

            leftBorder.width = 1;

            Rect cursorRect = rect;

            cursorRect.width = 10;
            cursorRect.x    -= 5;

            if (Event.current.type == EventType.MouseDown && cursorRect.Contains(Event.current.mousePosition))
            {
                resize = true;
                Event.current.Use();
            }

            if (resize && Event.current.type == EventType.MouseDrag)
            {
                normalizedPosition = Event.current.mousePosition.x / EditorGUIUtility.currentViewWidth;
                Event.current.Use();

                if (Event.current.type == EventType.MouseDrag)
                {
                    host.Repaint();
                }
            }

            if (Event.current.type == EventType.MouseUp)
            {
                resize = false;
            }

            EditorGUIUtility.AddCursorRect(cursorRect, MouseCursor.ResizeHorizontal);

            rect = rect.Expand(-4);
            GUILayout.BeginArea(rect);

            float labelWidth = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = 100;

            EditorGUILayout.LabelField("UV", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;

            UVWindowSettings.uvAlpha.value = EditorGUILayout.Slider("Alpha", UVWindowSettings.uvAlpha, 0f, 1f);
            UVWindowSettings.uvColor.value = EditorGUILayout.ColorField(
                new GUIContent("Color"),
                UVWindowSettings.uvColor,
                showEyedropper: true,
                showAlpha: false,
                hdr: false);

            EditorGUI.BeginChangeCheck();
            uvChannel = (UVChannel)EditorGUILayout.EnumPopup("Channel", uvChannel);
            if (EditorGUI.EndChangeCheck())
            {
                if (meshSource != null)
                {
                    meshSource.Mesh.GetUVs((int)uvChannel, uvBuffer);

                    if (uvBuffer.Count == 0)
                    {
                        host.ShowNotification(new GUIContent($"No {uvChannel.ToString()} found."));
                    }
                    else
                    {
                        host.RemoveNotification();
                    }
                }
            }

            EditorGUI.indentLevel--;

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Texture", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;

            EditorGUI.BeginChangeCheck();
            UVWindowSettings.textureAlpha.value = EditorGUILayout.Slider("Alpha", UVWindowSettings.textureAlpha, 0f, 1f);
            if (EditorGUI.EndChangeCheck())
            {
                previewMaterial.SetFloat("_Alpha", UVWindowSettings.textureAlpha);
            }

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Color");

            EditorGUI.BeginChangeCheck();
            colorChannel = (ColorChannel)GUILayout.Toolbar((int)colorChannel, colorChannelLabels, GUI.skin.button, GUI.ToolbarButtonSize.FitToContents);
            if (EditorGUI.EndChangeCheck())
            {
                UpdatePreviewMaterialColorMask();
            }

            EditorGUILayout.EndHorizontal();

            using (new EditorGUI.DisabledScope(meshSource.HasMaterial == false))
            {
                if (meshSource.HasMaterial)
                {
                    string[] names = meshSource.Material.GetTexturePropertyNames()
                                     .Where(x => meshSource.Material.HasProperty(x)).ToArray();

                    int selectedIndex = System.Array.IndexOf(names, texturePropertyName);

                    if (selectedIndex == -1)
                    {
                        selectedIndex = 0;
                    }

                    EditorGUI.BeginChangeCheck();
                    selectedIndex = EditorGUILayout.Popup("Source Map", selectedIndex, names);
                    if (EditorGUI.EndChangeCheck())
                    {
                        texturePropertyName = names[selectedIndex];
                    }
                }
                else
                {
                    EditorGUILayout.LabelField("Source Map", "<None>");
                }
            }

            EditorGUI.indentLevel--;

            EditorGUIUtility.labelWidth = labelWidth;
            GUILayout.EndArea();

            EditorGUI.DrawRect(leftBorder, new Color32(35, 35, 35, 255));
        }
Exemplo n.º 24
0
 	public static void ChangeMeshColor (Mesh mesh, ColorChannel channel, byte value)
 	{
 		Color32[] colors = mesh.colors32;
 		int length = mesh.vertexCount;
 		if (colors.Length != length)
 			colors = new Color32[length];
 		
 		switch (channel) {
 		case ColorChannel.RED:
 			for (int i = 0; i < length; i++) {
 				colors [i].r = value;
 			}
 			break;
 		case ColorChannel.GREEN:
 			for (int i = 0; i < length; i++) {
 				colors [i].g = value;
 			}
 			break;	
 		case ColorChannel.BLUE:
 			for (int i = 0; i < length; i++) {
 				colors [i].b = value;
 			}
 			break;
 		case ColorChannel.ALPHA:
 			for (int i = 0; i < length; i++) {
 				colors [i].a = value;
 			}
 			break;	
 		}		
 		mesh.colors32 = colors;
 	}
Exemplo n.º 25
0
 private void InitPicture()
 {
     _red   = new ColorChannel(GetRealWidth(), GetRealHeight());
     _green = new ColorChannel(GetRealWidth(), GetRealHeight());
     _blue  = new ColorChannel(GetRealWidth(), GetRealHeight());
 }
Exemplo n.º 26
0
 public static string GetAsShaderString(ColorChannel channel)
 {
     return channel.ToString().ToLowerInvariant();
 }
Exemplo n.º 27
0
 private RGBImage(ColorChannel r, ColorChannel g, ColorChannel b, int originalWidth, int originalHeight) :
     base(r, g, b)
 {
     OriginalWidth  = originalWidth;
     OriginalHeight = originalHeight;
 }
Exemplo n.º 28
0
 /**
  * Sets the color channel to read to and write from
  * @param color Color channel to interact with
  * @return A data writer to set channel properties
  */
 public abstract ChannelDataWriter setColorChannel(ColorChannel color);
Exemplo n.º 29
0
 	public static void ChangeMeshColor (Mesh mesh, ColorChannel channel, float value)
 	{
 		ChangeMeshColor (mesh, channel, (byte)Mathf.RoundToInt (Mathf.Lerp (0, 255, value)));
 	}
 public ComputeTextureScalar(Texture texture, ColorChannel channel) : this(texture)
 {
     Channel = channel;
 }
Exemplo n.º 31
0
 public byte GetValue(ColorChannel ch, int index)
 {
     return(GetChannelData(LinkChannels ? Channel : ch)[index]);
 }
Exemplo n.º 32
0
 public static ImageFilter CreateDisplacementMapEffect(ColorChannel xChannelSelector, ColorChannel yChannelSelector, float scale, ImageFilter displacement, ImageFilter?input = null, CropRect?cropRect = null)
 {
     return(new DisplacementMapEffectImageFilter()
     {
         XChannelSelector = xChannelSelector,
         YChannelSelector = yChannelSelector,
         Scale = scale,
         Displacement = displacement,
         Input = input,
         CropRect = cropRect
     });
 }
Exemplo n.º 33
0
        public static ColorOrNormalised ToColorOrNormalised(this IIfcColourOrFactor rgbOrFactor, ColorChannel colorChannel, float alpha = 1.0f)
        {
            if (null == rgbOrFactor)
            {
                return(null);
            }

            var color = new ColorOrNormalised();

            if (rgbOrFactor is IExpressRealType real)
            {
                color.Normalised = (float)real.Value;
            }
            else if (rgbOrFactor is IIfcColourRgb rgb)
            {
                color.Color = rgb.ToColor(alpha);
            }
            return(color);
        }
        /// <summary>
        /// Generates a new bitmap of the specified size by changing a specific color channel.
        /// This will produce a gradient representing all possible differences of that color channel.
        /// </summary>
        /// <param name="width">The pixel width (X, horizontal) of the resulting bitmap.</param>
        /// <param name="height">The pixel height (Y, vertical) of the resulting bitmap.</param>
        /// <param name="orientation">The orientation of the resulting bitmap (gradient direction).</param>
        /// <param name="colorRepresentation">The color representation being used: RGBA or HSVA.</param>
        /// <param name="channel">The specific color channel to vary.</param>
        /// <param name="baseHsvColor">The base HSV color used for channels not being changed.</param>
        /// <param name="checkerColor">The color of the checker background square.</param>
        /// <param name="isAlphaMaxForced">Fix the alpha channel value to maximum during calculation.
        /// This will remove any alpha/transparency from the other channel backgrounds.</param>
        /// <param name="isSaturationValueMaxForced">Fix the saturation and value channels to maximum
        /// during calculation in HSVA color representation.
        /// This will ensure colors are always discernible regardless of saturation/value.</param>
        /// <returns>A new bitmap representing a gradient of color channel values.</returns>
        public static async Task <byte[]> CreateChannelBitmapAsync(
            int width,
            int height,
            Orientation orientation,
            ColorRepresentation colorRepresentation,
            ColorChannel channel,
            HsvColor baseHsvColor,
            Color?checkerColor,
            bool isAlphaMaxForced,
            bool isSaturationValueMaxForced)
        {
            if (width == 0 || height == 0)
            {
                return(null);
            }

            var bitmap = await Task.Run <byte[]>(async() =>
            {
                int pixelDataIndex = 0;
                double channelStep;
                byte[] bgraPixelData;
                byte[] bgraCheckeredPixelData = null;
                Color baseRgbColor            = Colors.White;
                Color rgbColor;
                int bgraPixelDataHeight;
                int bgraPixelDataWidth;

                // Allocate the buffer
                // BGRA formatted color channels 1 byte each (4 bytes in a pixel)
                bgraPixelData       = new byte[width * height * 4];
                bgraPixelDataHeight = height * 4;
                bgraPixelDataWidth  = width * 4;

                // Maximize alpha channel value
                if (isAlphaMaxForced &&
                    channel != ColorChannel.Alpha)
                {
                    baseHsvColor = new HsvColor()
                    {
                        H = baseHsvColor.H,
                        S = baseHsvColor.S,
                        V = baseHsvColor.V,
                        A = 1.0
                    };
                }

                // Convert HSV to RGB once
                if (colorRepresentation == ColorRepresentation.Rgba)
                {
                    baseRgbColor = Uwp.Helpers.ColorHelper.FromHsv(
                        baseHsvColor.H,
                        baseHsvColor.S,
                        baseHsvColor.V,
                        baseHsvColor.A);
                }

                // Maximize Saturation and Value channels when in HSVA mode
                if (isSaturationValueMaxForced &&
                    colorRepresentation == ColorRepresentation.Hsva &&
                    channel != ColorChannel.Alpha)
                {
                    switch (channel)
                    {
                    case ColorChannel.Channel1:
                        baseHsvColor = new HsvColor()
                        {
                            H = baseHsvColor.H,
                            S = 1.0,
                            V = 1.0,
                            A = baseHsvColor.A
                        };
                        break;

                    case ColorChannel.Channel2:
                        baseHsvColor = new HsvColor()
                        {
                            H = baseHsvColor.H,
                            S = baseHsvColor.S,
                            V = 1.0,
                            A = baseHsvColor.A
                        };
                        break;

                    case ColorChannel.Channel3:
                        baseHsvColor = new HsvColor()
                        {
                            H = baseHsvColor.H,
                            S = 1.0,
                            V = baseHsvColor.V,
                            A = baseHsvColor.A
                        };
                        break;
                    }
                }

                // Create a checkered background
                if (checkerColor != null)
                {
                    bgraCheckeredPixelData = await CreateCheckeredBitmapAsync(
                        width,
                        height,
                        checkerColor.Value);
                }

                // Create the color channel gradient
                if (orientation == Orientation.Horizontal)
                {
                    // Determine the numerical increment of the color steps within the channel
                    if (colorRepresentation == ColorRepresentation.Hsva)
                    {
                        if (channel == ColorChannel.Channel1)
                        {
                            channelStep = 360.0 / width;
                        }
                        else
                        {
                            channelStep = 1.0 / width;
                        }
                    }
                    else
                    {
                        channelStep = 255.0 / width;
                    }

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            if (y == 0)
                            {
                                rgbColor = GetColor(x *channelStep);

                                // Get a new color
                                bgraPixelData[pixelDataIndex + 0] = Convert.ToByte(rgbColor.B *rgbColor.A / 255);
                                bgraPixelData[pixelDataIndex + 1] = Convert.ToByte(rgbColor.G *rgbColor.A / 255);
                                bgraPixelData[pixelDataIndex + 2] = Convert.ToByte(rgbColor.R *rgbColor.A / 255);
                                bgraPixelData[pixelDataIndex + 3] = rgbColor.A;
                            }
                            else
                            {
                                // Use the color in the row above
                                // Remember the pixel data is 1 dimensional instead of 2
                                bgraPixelData[pixelDataIndex + 0] = bgraPixelData[pixelDataIndex + 0 - bgraPixelDataWidth];
                                bgraPixelData[pixelDataIndex + 1] = bgraPixelData[pixelDataIndex + 1 - bgraPixelDataWidth];
                                bgraPixelData[pixelDataIndex + 2] = bgraPixelData[pixelDataIndex + 2 - bgraPixelDataWidth];
                                bgraPixelData[pixelDataIndex + 3] = bgraPixelData[pixelDataIndex + 3 - bgraPixelDataWidth];
                            }

                            pixelDataIndex += 4;
                        }
                    }
                }
                else
                {
                    // Determine the numerical increment of the color steps within the channel
                    if (colorRepresentation == ColorRepresentation.Hsva)
                    {
                        if (channel == ColorChannel.Channel1)
                        {
                            channelStep = 360.0 / height;
                        }
                        else
                        {
                            channelStep = 1.0 / height;
                        }
                    }
                    else
                    {
                        channelStep = 255.0 / height;
                    }

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            if (x == 0)
                            {
                                // The lowest channel value should be at the 'bottom' of the bitmap
                                rgbColor = GetColor((height - 1 - y) * channelStep);

                                // Get a new color
                                bgraPixelData[pixelDataIndex + 0] = Convert.ToByte(rgbColor.B *rgbColor.A / 255);
                                bgraPixelData[pixelDataIndex + 1] = Convert.ToByte(rgbColor.G *rgbColor.A / 255);
                                bgraPixelData[pixelDataIndex + 2] = Convert.ToByte(rgbColor.R *rgbColor.A / 255);
                                bgraPixelData[pixelDataIndex + 3] = rgbColor.A;
                            }
                            else
                            {
                                // Use the color in the column to the left
                                // Remember the pixel data is 1 dimensional instead of 2
                                bgraPixelData[pixelDataIndex + 0] = bgraPixelData[pixelDataIndex - 4];
                                bgraPixelData[pixelDataIndex + 1] = bgraPixelData[pixelDataIndex - 3];
                                bgraPixelData[pixelDataIndex + 2] = bgraPixelData[pixelDataIndex - 2];
                                bgraPixelData[pixelDataIndex + 3] = bgraPixelData[pixelDataIndex - 1];
                            }

                            pixelDataIndex += 4;
                        }
                    }
                }

                // Composite the checkered background with color channel gradient for final result
                // The height/width are not checked as both bitmaps were built with the same values
                if ((checkerColor != null) &&
                    (bgraCheckeredPixelData != null))
                {
                    pixelDataIndex = 0;
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            /* The following algorithm is used to blend the two bitmaps creating the final composite.
                             * In this formula, pixel data is normalized 0..1, actual pixel data is in the range 0..255.
                             * The color channel gradient should apply OVER the checkered background.
                             *
                             * R =  R0 * A0 * (1 - A1) + R1 * A1  =  RA0 * (1 - A1) + RA1
                             * G =  G0 * A0 * (1 - A1) + G1 * A1  =  GA0 * (1 - A1) + GA1
                             * B =  B0 * A0 * (1 - A1) + B1 * A1  =  BA0 * (1 - A1) + BA1
                             * A =  A0 * (1 - A1) + A1            =  A0 * (1 - A1) + A1
                             *
                             * Considering only the red channel, some algebraic transformation is applied to
                             * make the math quicker to solve.
                             *
                             * => ((RA0 / 255.0) * (1.0 - A1 / 255.0) + (RA1 / 255.0)) * 255.0
                             * => ((RA0 * 255) - (RA0 * A1) + (RA1 * 255)) / 255
                             */

                            // Bottom layer
                            byte rXa0 = bgraCheckeredPixelData[pixelDataIndex + 2];
                            byte gXa0 = bgraCheckeredPixelData[pixelDataIndex + 1];
                            byte bXa0 = bgraCheckeredPixelData[pixelDataIndex + 0];
                            byte a0   = bgraCheckeredPixelData[pixelDataIndex + 3];

                            // Top layer
                            byte rXa1 = bgraPixelData[pixelDataIndex + 2];
                            byte gXa1 = bgraPixelData[pixelDataIndex + 1];
                            byte bXa1 = bgraPixelData[pixelDataIndex + 0];
                            byte a1   = bgraPixelData[pixelDataIndex + 3];

                            bgraPixelData[pixelDataIndex + 0] = Convert.ToByte(((bXa0 * 255) - (bXa0 * a1) + (bXa1 * 255)) / 255);
                            bgraPixelData[pixelDataIndex + 1] = Convert.ToByte(((gXa0 * 255) - (gXa0 * a1) + (gXa1 * 255)) / 255);
                            bgraPixelData[pixelDataIndex + 2] = Convert.ToByte(((rXa0 * 255) - (rXa0 * a1) + (rXa1 * 255)) / 255);
                            bgraPixelData[pixelDataIndex + 3] = Convert.ToByte(((a0 * 255) - (a0 * a1) + (a1 * 255)) / 255);

                            pixelDataIndex += 4;
                        }
                    }
                }

                Color GetColor(double channelValue)
                {
                    Color newRgbColor = Colors.White;

                    switch (channel)
                    {
                    case ColorChannel.Channel1:
                        {
                            if (colorRepresentation == ColorRepresentation.Hsva)
                            {
                                // Sweep hue
                                newRgbColor = Uwp.Helpers.ColorHelper.FromHsv(
                                    MathEx.Clamp(channelValue, 0.0, 360.0),
                                    baseHsvColor.S,
                                    baseHsvColor.V,
                                    baseHsvColor.A);
                            }
                            else
                            {
                                // Sweep red
                                newRgbColor = new Color
                                {
                                    R = Convert.ToByte(MathEx.Clamp(channelValue, 0.0, 255.0)),
                                    G = baseRgbColor.G,
                                    B = baseRgbColor.B,
                                    A = baseRgbColor.A
                                };
                            }

                            break;
                        }

                    case ColorChannel.Channel2:
                        {
                            if (colorRepresentation == ColorRepresentation.Hsva)
                            {
                                // Sweep saturation
                                newRgbColor = Uwp.Helpers.ColorHelper.FromHsv(
                                    baseHsvColor.H,
                                    MathEx.Clamp(channelValue, 0.0, 1.0),
                                    baseHsvColor.V,
                                    baseHsvColor.A);
                            }
                            else
                            {
                                // Sweep green
                                newRgbColor = new Color
                                {
                                    R = baseRgbColor.R,
                                    G = Convert.ToByte(MathEx.Clamp(channelValue, 0.0, 255.0)),
                                    B = baseRgbColor.B,
                                    A = baseRgbColor.A
                                };
                            }

                            break;
                        }

                    case ColorChannel.Channel3:
                        {
                            if (colorRepresentation == ColorRepresentation.Hsva)
                            {
                                // Sweep value
                                newRgbColor = Uwp.Helpers.ColorHelper.FromHsv(
                                    baseHsvColor.H,
                                    baseHsvColor.S,
                                    MathEx.Clamp(channelValue, 0.0, 1.0),
                                    baseHsvColor.A);
                            }
                            else
                            {
                                // Sweep blue
                                newRgbColor = new Color
                                {
                                    R = baseRgbColor.R,
                                    G = baseRgbColor.G,
                                    B = Convert.ToByte(MathEx.Clamp(channelValue, 0.0, 255.0)),
                                    A = baseRgbColor.A
                                };
                            }

                            break;
                        }

                    case ColorChannel.Alpha:
                        {
                            if (colorRepresentation == ColorRepresentation.Hsva)
                            {
                                // Sweep alpha
                                newRgbColor = Uwp.Helpers.ColorHelper.FromHsv(
                                    baseHsvColor.H,
                                    baseHsvColor.S,
                                    baseHsvColor.V,
                                    MathEx.Clamp(channelValue, 0.0, 1.0));
                            }
                            else
                            {
                                // Sweep alpha
                                newRgbColor = new Color
                                {
                                    R = baseRgbColor.R,
                                    G = baseRgbColor.G,
                                    B = baseRgbColor.B,
                                    A = Convert.ToByte(MathEx.Clamp(channelValue, 0.0, 255.0))
                                };
                            }

                            break;
                        }
                    }

                    return(newRgbColor);
                }

                return(bgraPixelData);
            });

            return(bitmap);
        }
Exemplo n.º 35
0
 byte GetChannelValue(Color color, ColorChannel channel) => channel switch
 {
Exemplo n.º 36
0
        public static Bitmap EkvilizeCustom(Bitmap bmp, int[] histogramValues, ColorChannel chanel)
        {
            Bitmap resultBmp = new Bitmap(bmp);

            int[] cdf    = new int[histogramValues.Length];
            int   cdfMin = 0;

            for (int i = 0; i < histogramValues.Length; i++)
            {
                cdf[i]  = (i == 0) ? 0 : cdf[i - 1];
                cdf[i] += histogramValues[i];

                if (cdf[i] > 0 && (cdfMin == 0 || cdf[i] < cdfMin))
                {
                    cdfMin = cdf[i];
                }
            }

            Color[][] colors      = ImageExtension.GetolorMatrix(bmp);
            int       countPixels = colors.Length * colors[0].Length;

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[i].Length; j++)
                {
                    byte R = colors[i][j].R;
                    byte G = colors[i][j].G;
                    byte B = colors[i][j].B;

                    switch (chanel)
                    {
                    case ColorChannel.Blue:
                    {
                        B = (byte)((int)((((double)(cdf[colors[i][j].B] - cdfMin)) / ((double)countPixels))
                                         * ((double)(CountColors - 1))));
                        break;
                    }

                    case ColorChannel.Green:
                    {
                        G = (byte)((int)((((double)(cdf[colors[i][j].G] - cdfMin)) / ((double)countPixels))
                                         * ((double)(CountColors - 1))));
                        break;
                    }

                    case ColorChannel.Red:
                    {
                        R = (byte)((int)((((double)(cdf[colors[i][j].R] - cdfMin)) / ((double)countPixels))
                                         * ((double)(CountColors - 1))));
                        break;
                    }

                    default:
                        break;
                    }

                    resultBmp.SetPixel(i, j, Color.FromArgb(colors[i][j].A, R, G, B));
                }
            }

            return(resultBmp);
        }
Exemplo n.º 37
0
 public ViewModel() : base()
 {
     chanel = ColorChannel.Green;
 }
Exemplo n.º 38
0
 public int obtenerPosiArre(int x, int y, ColorChannel c)
 {
     return(Convert.ToInt32((RowSizeBytes * y + x * 4) + c));
 }
Exemplo n.º 39
0
 public void CreateColorChannel()
 {
     colorChannel = new ColorChannel(width, height, partitionSizeX, partitionSizeY);
     colorChannel.Create();
 }
Exemplo n.º 40
0
        public static Bitmap ApplyMask(Bitmap img, double[][] mask, ColorChannel colorChannel, int treshe = 0)
        {
            Bitmap resBmp = new Bitmap(img);

            var col    = (int)colorChannel;
            var colors = ImageExtension.GetolorMatrix(img);
            int r_tr   = 0;
            int g_tr   = 0;
            int b_tr   = 0;

            for (int _i = 0; _i < colors.Length; _i++)
            {
                for (int _j = 0; _j < colors[0].Length; _j++)
                {
                    if (colors[_i][_j].R > r_tr)
                    {
                        r_tr = colors[_i][_j].R;
                    }
                    if (colors[_i][_j].G > g_tr)
                    {
                        g_tr = colors[_i][_j].G;
                    }
                    if (colors[_i][_j].B > b_tr)
                    {
                        b_tr = colors[_i][_j].B;
                    }
                }
            }

            r_tr /= 2;
            g_tr /= 2;
            b_tr /= 2;
            int tr = (treshe != 0) ? treshe : (r_tr + g_tr + b_tr) / 3;

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[0].Length; j++)
                {
                    var itemMatrix = getItemMatrix(i, j, colors, mask.Length);
                    var newValue   = (byte)ItemCalculation(itemMatrix, mask, treshe, colorChannel);

                    Color newColor;

                    switch ((int)colorChannel)
                    {
                    //blue
                    case 0:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, colors[i][j].R, colors[i][j].G, newValue);
                        break;
                    }

                    // green
                    case 1:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, colors[i][j].R, newValue, colors[i][j].B);
                        break;
                    }

                    // red
                    case 2:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, newValue, colors[i][j].G, colors[i][j].B);
                        break;
                    }

                    default:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, colors[i][j].R, colors[i][j].G, colors[i][j].B);
                        break;
                    }
                    }

                    resBmp.SetPixel(i, j, newColor);
                }
            }

            return(resBmp);
        }