コード例 #1
0
        protected void Apply()
        {
            Cursor = Cursors.Wait;
            var sourceColor = ColorTranslator.FromHtml(SourceColor);
            var targetColor = ColorTranslator.FromHtml(ReplaceColor.Substring(ReplaceColor.IndexOf("#")).Replace("]", ""));

            foreach (var bmp in Images.Values)
            {
                for (var x = 0; x < bmp.Width; x++)
                {
                    for (var y = 0; y < bmp.Height; y++)
                    {
                        var pixel = bmp.GetPixel(x, y);

                        if (!UpThreshold && !DownThreshold &&
                            pixel.A == sourceColor.A &&
                            pixel.R == sourceColor.R &&
                            pixel.G == sourceColor.G &&
                            pixel.B == sourceColor.B)
                        {
                            bmp.SetPixel(x, y, targetColor);
                        }

                        else if (UpThreshold &&
                                 pixel.A >= sourceColor.A &&
                                 pixel.R >= sourceColor.R &&
                                 pixel.G >= sourceColor.G &&
                                 pixel.B >= sourceColor.B)
                        {
                            bmp.SetPixel(x, y, targetColor);
                        }

                        else if (DownThreshold &&
                                 pixel.A <= sourceColor.A &&
                                 pixel.R <= sourceColor.R &&
                                 pixel.G <= sourceColor.G &&
                                 pixel.B <= sourceColor.B)
                        {
                            bmp.SetPixel(x, y, targetColor);
                        }
                    }
                }
            }

            SaveCommand?.RaiseCanExecuteChanged();
            Cursor = Cursors.Arrow;
        }
コード例 #2
0
        /// <summary>
        /// Replaces a color within the current image.
        /// </summary>
        /// <param name="target">
        /// The target <see cref="System.Drawing.Color"/>.
        /// </param>
        /// <param name="replacement">
        /// The replacement <see cref="System.Drawing.Color"/>.
        /// </param>
        /// <param name="fuzziness">
        /// A value between 0 and 128 with which to alter the target detection accuracy.
        /// </param>
        /// <returns>
        /// The <see cref="ImageFactory"/>.
        /// </returns>
        public ImageFactory ReplaceColor(Color target, Color replacement, int fuzziness = 0)
        {
            // Sanitize the input.
            if (fuzziness < 0 || fuzziness > 128)
            {
                fuzziness = 0;
            }

            if (this.ShouldProcess && target != Color.Empty && replacement != Color.Empty)
            {
                ReplaceColor replaceColor = new ReplaceColor
                {
                    DynamicParameter = new Tuple <Color, Color, int>(target, replacement, fuzziness)
                };
                this.CurrentImageFormat.ApplyProcessor(replaceColor.ProcessImage, this);
            }

            return(this);
        }