Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.Threading.Tasks;
- using System.Threading;
- namespace ImatgesIColoraines
- {
- public delegate void ActualizarProgress();
- public enum Estil
- { EscalaDeGrisos, Sepia, Vermell, Verd, Blau, Invers }
- struct Pixel
- {
- public byte red;
- public byte green;
- public byte blue;
- }
- public static class Colorizer
- {
- public static event ActualizarProgress UpdProg;
- public static Bitmap ChangeColor(Bitmap img, Estil estil)
- {
- int y = 0, x = 0;
- Pixel original;
- Color cambiat = new Color();
- Bitmap nImg =(Bitmap) img.Clone();
- while (y < img.Height)
- {
- original = GetPixel(img, x, y);
- switch (estil)
- {
- case Estil.EscalaDeGrisos:
- cambiat = BlackWhite(GetPixel(img, x, y));
- break;
- case Estil.Invers:
- cambiat = Inverter(GetPixel(img, x, y));
- break;
- case Estil.Sepia:
- cambiat = Sepia(GetPixel(img, x, y));
- break;
- case Estil.Vermell:
- cambiat = RedAxe(GetPixel(img, x, y));
- break;
- case Estil.Verd:
- cambiat = GreenForest(GetPixel(img, x, y));
- break;
- case Estil.Blau:
- cambiat = BlueStar(GetPixel(img, x, y));
- break;
- }
- nImg.SetPixel(x, y, cambiat);
- x++; //y++;
- if (x >= img.Width)
- {
- x = 0;
- y++;
- if (UpdProg != null && y % 3 == 0)
- UpdProg();
- }
- }
- return nImg;
- }
- private static Pixel GetPixel(Bitmap img, int x, int y)
- {
- Pixel pix;
- pix.red = img.GetPixel(x, y).R;
- pix.green = img.GetPixel(x, y).G;
- pix.blue = img.GetPixel(x, y).B;
- return pix;
- }
- private static Color BlackWhite(Pixel pix)
- {
- byte resultat = Convert.ToByte((pix.red + pix.green + pix.blue) / 3);
- return Color.FromArgb(255,resultat,resultat,resultat);
- }
- private static Color Inverter(Pixel pix)
- {
- pix.red = (byte) (255 - pix.red);
- pix.green = (byte)(255 - pix.green);
- pix.blue = (byte)(255 - pix.blue);
- return Color.FromArgb(255, pix.red, pix.green, pix.blue);
- }
- private static Color Sepia(Pixel pix)
- {
- double pixelColor;
- pixelColor = (pix.red * .393) + (pix.green * .769) + (pix.blue * .189);
- pix.red = Convert.ToByte(pixelColor > 255 ? 255 : pixelColor);
- pixelColor = (pix.red * .349) + (pix.green * .686) + (pix.blue * .168);
- pix.green = Convert.ToByte(pixelColor > 255 ? 255 : pixelColor);
- pixelColor = (pix.red * .272) + (pix.green * .534) + (pix.blue * .131);
- pix.blue = Convert.ToByte(pixelColor > 255 ? 255 : pixelColor);
- return Color.FromArgb(255, pix.red, pix.green, pix.blue);
- }
- private static Color RedAxe(Pixel pix)
- {
- pix.green = (byte)0;
- pix.blue = (byte)0;
- return Color.FromArgb(255, pix.red, pix.green, pix.blue);
- }
- private static Color GreenForest(Pixel pix)
- {
- pix.red = (byte)0;
- pix.blue = (byte)0;
- return Color.FromArgb(255, pix.red, pix.green, pix.blue);
- }
- private static Color BlueStar(Pixel pix)
- {
- pix.red = (byte)0;
- pix.green = (byte)0;
- return Color.FromArgb(255, pix.red, pix.green, pix.blue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment