Advertisement
Guest User

ChangeColorActivity

a guest
Jan 13th, 2018
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Activities;
  6. using System.Drawing;
  7. using System.ComponentModel;
  8.  
  9. namespace CleanImageActivities
  10. {
  11.  
  12.     public sealed class ChangeColorOnImage : CodeActivity
  13.     {
  14.         [RequiredArgument]
  15.         [Category("Input")]
  16.         [Description("Original image object to change color from.")]
  17.         public InArgument<Image> ImageToConvert { get; set; }
  18.  
  19.         [RequiredArgument]
  20.         [Category("Input")]
  21.         [Description("System.Drawing.Color describing color range to change.")]
  22.         public InArgument<Color> ColorToChange { get; set; }
  23.  
  24.         [RequiredArgument]
  25.         [Category("Input")]
  26.         [Description("System.Drawing.Color describing color range to change ColorToChange to.")]
  27.         public InArgument<Color> ColorToSet { get; set; }
  28.  
  29.         [Category("Skip borders")]
  30.         public InArgument<int> SkipPixelsFromLeft { get; set; }
  31.         [Category("Skip borders")]
  32.         public InArgument<int> SkipPixelsFromTop { get; set; }
  33.         [Category("Skip borders")]
  34.         public InArgument<int> SkipPixelsFromRight { get; set; }
  35.         [Category("Skip borders")]
  36.         public InArgument<int> SkipPixelsFromBottom { get; set; }
  37.  
  38.         [Category("Output")]
  39.         [Description("New System.Drawing.Image generated from ImageToConvert.")]
  40.         public OutArgument<Image> ChangedImage { get; set; }
  41.  
  42.         protected override void Execute(CodeActivityContext context)
  43.         {
  44.             Image originalImage = context.GetValue(this.ImageToConvert);
  45.             Color undesiredColor = context.GetValue(this.ColorToChange);
  46.             Color desiredColor = context.GetValue(this.ColorToSet);
  47.  
  48.             // Set number of pixels to skip
  49.             int skipLeft = context.GetValue(this.SkipPixelsFromLeft);
  50.             int skipTop = context.GetValue(this.SkipPixelsFromTop);
  51.             int skipRight = context.GetValue(this.SkipPixelsFromRight);
  52.             int skipBottom = context.GetValue(this.SkipPixelsFromBottom);
  53.  
  54.             Bitmap newBitmap = new Bitmap(originalImage);
  55.  
  56.             for (int x = 0 + skipLeft; x < newBitmap.Width - skipRight; x++)
  57.             {
  58.                 for (int y = 0 + skipBottom; y < newBitmap.Height - skipTop; y++)
  59.                 {
  60.                     var originalColor = newBitmap.GetPixel(x, y);
  61.                     if (ClassifyAsGeneralColor(originalColor) == undesiredColor)
  62.                         newBitmap.SetPixel(x, y, desiredColor);
  63.                 }
  64.             }
  65.  
  66.             ChangedImage.Set(context, newBitmap);
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Classifies given Color to general range of colors. Shamelessly nicked from https://stackoverflow.com/a/8457981 .
  71.         /// </summary>
  72.         /// <param name="c">Color to classify.</param>
  73.         /// <returns>General Color.</returns>
  74.         Color ClassifyAsGeneralColor(Color c)
  75.         {
  76.             float hue = c.GetHue();
  77.             float sat = c.GetSaturation();
  78.             float lgt = c.GetBrightness();
  79.  
  80.             if (lgt < 0.2) return Color.Black;
  81.             if (lgt > 0.8) return Color.White;
  82.  
  83.             if (sat < 0.25) return Color.Gray;
  84.  
  85.             if (hue < 30) return Color.Red;
  86.             if (hue < 90) return Color.Yellow;
  87.             if (hue < 150) return Color.Green;
  88.             if (hue < 210) return Color.Cyan;
  89.             if (hue < 270) return Color.Blue;
  90.             if (hue < 330) return Color.Magenta;
  91.             return Color.Red;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement