kurobyte

Colorizer Class

Feb 20th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8.  
  9. namespace ImatgesIColoraines
  10. {
  11.     public delegate void ActualizarProgress();
  12.    
  13.     public enum Estil
  14.     { EscalaDeGrisos, Sepia, Vermell, Verd, Blau, Invers }
  15.  
  16.     struct Pixel
  17.     {
  18.         public byte red;
  19.         public byte green;
  20.         public byte blue;
  21.     }
  22.  
  23.     public static class Colorizer
  24.     {
  25.         public static event ActualizarProgress UpdProg;
  26.  
  27.         public static Bitmap ChangeColor(Bitmap img, Estil estil)
  28.         {
  29.             int y = 0, x = 0;
  30.             Pixel original;
  31.             Color cambiat = new Color();
  32.             Bitmap nImg =(Bitmap) img.Clone();
  33.  
  34.             while (y < img.Height)
  35.             {
  36.  
  37.                 original = GetPixel(img, x, y);
  38.                 switch (estil)
  39.                 {
  40.                     case Estil.EscalaDeGrisos:
  41.                         cambiat =  BlackWhite(GetPixel(img, x, y));
  42.                         break;
  43.                     case Estil.Invers:
  44.                         cambiat = Inverter(GetPixel(img, x, y));
  45.                         break;
  46.                     case Estil.Sepia:
  47.                         cambiat = Sepia(GetPixel(img, x, y));
  48.                         break;
  49.                     case Estil.Vermell:
  50.                         cambiat = RedAxe(GetPixel(img, x, y));
  51.                         break;
  52.                     case Estil.Verd:
  53.                         cambiat = GreenForest(GetPixel(img, x, y));
  54.                         break;
  55.                     case Estil.Blau:
  56.                         cambiat = BlueStar(GetPixel(img, x, y));
  57.                         break;
  58.                 }
  59.  
  60.                 nImg.SetPixel(x, y, cambiat);
  61.  
  62.                 x++; //y++;
  63.                 if (x >= img.Width)
  64.                 {
  65.                     x = 0;
  66.                     y++;
  67.                     if (UpdProg != null && y % 3 == 0)
  68.                         UpdProg();
  69.                 }
  70.  
  71.             }
  72.  
  73.             return nImg;
  74.         }
  75.  
  76.         private static Pixel GetPixel(Bitmap img, int x, int y)
  77.         {
  78.             Pixel pix;
  79.             pix.red = img.GetPixel(x, y).R;
  80.             pix.green = img.GetPixel(x, y).G;
  81.             pix.blue = img.GetPixel(x, y).B;
  82.  
  83.             return pix;
  84.         }
  85.  
  86.         private static Color BlackWhite(Pixel pix)
  87.         {
  88.             byte resultat = Convert.ToByte((pix.red + pix.green + pix.blue) / 3);
  89.            
  90.             return Color.FromArgb(255,resultat,resultat,resultat);
  91.         }
  92.  
  93.         private static Color Inverter(Pixel pix)
  94.         {
  95.             pix.red = (byte) (255 - pix.red);
  96.             pix.green = (byte)(255 - pix.green);
  97.             pix.blue = (byte)(255 - pix.blue);
  98.  
  99.             return Color.FromArgb(255, pix.red, pix.green, pix.blue);
  100.         }
  101.  
  102.         private static Color Sepia(Pixel pix)
  103.         {
  104.             double pixelColor;
  105.             pixelColor = (pix.red * .393) + (pix.green * .769) + (pix.blue * .189);
  106.             pix.red = Convert.ToByte(pixelColor > 255 ? 255 : pixelColor);
  107.             pixelColor = (pix.red * .349) + (pix.green * .686) + (pix.blue * .168);
  108.             pix.green = Convert.ToByte(pixelColor > 255 ? 255 : pixelColor);
  109.             pixelColor = (pix.red * .272) + (pix.green * .534) + (pix.blue * .131);
  110.             pix.blue = Convert.ToByte(pixelColor > 255 ? 255 : pixelColor);
  111.  
  112.             return Color.FromArgb(255, pix.red, pix.green, pix.blue);
  113.         }
  114.  
  115.         private static Color RedAxe(Pixel pix)
  116.         {
  117.             pix.green = (byte)0;
  118.             pix.blue = (byte)0;
  119.  
  120.             return Color.FromArgb(255, pix.red, pix.green, pix.blue);
  121.         }
  122.  
  123.         private static Color GreenForest(Pixel pix)
  124.         {
  125.             pix.red = (byte)0;
  126.             pix.blue = (byte)0;
  127.  
  128.             return Color.FromArgb(255, pix.red, pix.green, pix.blue);
  129.         }
  130.  
  131.         private static Color BlueStar(Pixel pix)
  132.         {
  133.             pix.red = (byte)0;
  134.             pix.green = (byte)0;
  135.  
  136.             return Color.FromArgb(255, pix.red, pix.green, pix.blue);
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment