Advertisement
Yassine_Abbani

Color Palette [Tools], [Costume Control]

May 22nd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.11 KB | None | 0 0
  1.  
  2. /// <summary>
  3. /// CePalette
  4. /// Color Palette Creater
  5. /// Author : Yassine Abbani
  6. /// </summary>
  7.  
  8. #region NameSpaces
  9.  
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Drawing;
  17.  
  18. #endregion
  19.  
  20. #region Control
  21.  
  22. public class Ce_Palette : Control
  23. {
  24.  
  25.     #region Variables
  26.  
  27.     private Rectangle R1, R2, R3, R4, R5;
  28.     private Bitmap IMG;
  29.     private Color _Color1 = ColorTranslator.FromHtml("#E2C2C6");
  30.     private Color _Color2 = ColorTranslator.FromHtml("#BA93A4");
  31.     private Color _Color3 = ColorTranslator.FromHtml("#935B86");
  32.     private Color _Color4 = ColorTranslator.FromHtml("#6B346B");
  33.     private Color _Color5 = ColorTranslator.FromHtml("#3D1A4C");
  34.     private int _RectWidths = 50;
  35.     private bool _Savable = false;
  36.     private bool _ShowHTMLColor, _ShowRGBColor;
  37.  
  38.     #endregion
  39.  
  40.     #region Constructors
  41.  
  42.     public Ce_Palette()
  43.     {
  44.         SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint |
  45.              ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
  46.              ControlStyles.SupportsTransparentBackColor, true);
  47.         DoubleBuffered = true;
  48.         UpdateStyles();
  49.         BackColor = Color.Transparent;
  50.         Font = new Font("Arial", 6);
  51.         ShowHTMLColor = true;
  52.         ShowRGBColor = true;
  53.     }
  54.  
  55.     #endregion
  56.  
  57.     #region Properties
  58.  
  59.     public Color ColorA
  60.     {
  61.         get
  62.         {
  63.             return _Color1;
  64.         }
  65.         set
  66.         {
  67.             _Color1 = value;
  68.             Invalidate();
  69.         }
  70.     }
  71.  
  72.     public Color ColorB
  73.     {
  74.         get
  75.         {
  76.             return _Color2;
  77.         }
  78.         set
  79.         {
  80.             _Color2 = value;
  81.             Invalidate();
  82.         }
  83.     }
  84.  
  85.     public Color ColorC
  86.     {
  87.         get
  88.         {
  89.             return _Color3;
  90.         }
  91.         set
  92.         {
  93.             _Color3 = value;
  94.             Invalidate();
  95.         }
  96.     }
  97.  
  98.     public Color ColorD
  99.     {
  100.         get
  101.         {
  102.             return _Color4;
  103.         }
  104.         set
  105.         {
  106.             _Color4 = value;
  107.             Invalidate();
  108.         }
  109.     }
  110.  
  111.     public Color ColorE
  112.     {
  113.         get
  114.         {
  115.             return _Color5;
  116.         }
  117.         set
  118.         {
  119.             _Color5 = value;
  120.             Invalidate();
  121.         }
  122.     }
  123.  
  124.     public int RectWidths
  125.     {
  126.         get
  127.         {
  128.             return _RectWidths;
  129.         }
  130.         set
  131.         {
  132.             _RectWidths = value;
  133.             Invalidate();
  134.         }
  135.     }
  136.  
  137.     public bool ShowHTMLColor
  138.     {
  139.         get
  140.         {
  141.             return _ShowHTMLColor;
  142.         }
  143.         set
  144.         {
  145.             _ShowHTMLColor = value;
  146.             Invalidate();
  147.         }
  148.     }
  149.  
  150.     public bool ShowRGBColor
  151.     {
  152.         get
  153.         {
  154.             return _ShowRGBColor;
  155.         }
  156.         set
  157.         {
  158.             _ShowRGBColor = value;
  159.             Invalidate();
  160.         }
  161.     }
  162.  
  163.     public bool Savable
  164.     {
  165.         get
  166.         {
  167.             return _Savable;
  168.         }
  169.         set
  170.         {
  171.             _Savable = value;
  172.             Invalidate();
  173.         }
  174.     }
  175.  
  176.     #endregion
  177.  
  178.     #region Events
  179.  
  180.     protected override void OnClick(EventArgs e)
  181.     {
  182.         if (Savable)
  183.         {
  184.             using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Jpeg File(*.jpg)|*.jpg" })
  185.             {
  186.  
  187.                 if (sfd.ShowDialog() == DialogResult.OK)
  188.                 {
  189.                     IMG.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
  190.                 }
  191.  
  192.             }
  193.  
  194.         }
  195.         base.OnClick(e);
  196.     }
  197.  
  198.     #endregion
  199.  
  200.     #region Draw Control
  201.  
  202.     protected override void OnPaint(PaintEventArgs e)
  203.     {
  204.         Width = RectWidths * 5;
  205.         R1 = new Rectangle(0, 0, RectWidths, Height);
  206.         R2 = new Rectangle(RectWidths, 0, RectWidths, Height);
  207.         R3 = new Rectangle(RectWidths * 2, 0, RectWidths, Height);
  208.         R4 = new Rectangle(RectWidths * 3, 0, RectWidths, Height);
  209.         R5 = new Rectangle(RectWidths * 4, 0, RectWidths, Height);
  210.  
  211.         using (Bitmap B = new Bitmap(Width, Height))
  212.         using (Graphics G = Graphics.FromImage(B))
  213.         {
  214.  
  215.             G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  216.  
  217.             G.FillRectangle(new SolidBrush(ColorA), R1);
  218.             G.FillRectangle(new SolidBrush(ColorB), R2);
  219.             G.FillRectangle(new SolidBrush(ColorC), R3);
  220.             G.FillRectangle(new SolidBrush(ColorD), R4);
  221.             G.FillRectangle(new SolidBrush(ColorE), R5);
  222.  
  223.             if (ShowHTMLColor)
  224.             {
  225.                 G.DrawString(ColorTranslator.ToHtml(ColorA), Font, new SolidBrush(ColorB), new Rectangle(R1.X, R1.Height - 30, R1.Width, R1.Height), new StringFormat { Alignment = StringAlignment.Center });
  226.                 G.DrawString(ColorTranslator.ToHtml(ColorB), Font, new SolidBrush(ColorC), new Rectangle(R2.X, R2.Height - 30, R2.Width, R2.Height), new StringFormat { Alignment = StringAlignment.Center });
  227.                 G.DrawString(ColorTranslator.ToHtml(ColorC), Font, new SolidBrush(ColorD), new Rectangle(R3.X, R3.Height - 30, R3.Width, R3.Height), new StringFormat { Alignment = StringAlignment.Center });
  228.                 G.DrawString(ColorTranslator.ToHtml(ColorD), Font, new SolidBrush(ColorE), new Rectangle(R4.X, R4.Height - 30, R4.Width, R4.Height), new StringFormat { Alignment = StringAlignment.Center });
  229.                 G.DrawString(ColorTranslator.ToHtml(ColorE), Font, new SolidBrush(ColorD), new Rectangle(R5.X, R5.Height - 30, R5.Width, R5.Height), new StringFormat { Alignment = StringAlignment.Center });
  230.             }
  231.             if (ShowRGBColor)
  232.             {
  233.                 G.DrawString(GetRGBFromColor(ColorA), Font, new SolidBrush(ColorB), new Rectangle(R1.X, R1.Height - 20, R1.Width, R1.Height), new StringFormat { Alignment = StringAlignment.Center });
  234.                 G.DrawString(GetRGBFromColor(ColorB), Font, new SolidBrush(ColorC), new Rectangle(R2.X, R2.Height - 20, R2.Width, R2.Height), new StringFormat { Alignment = StringAlignment.Center });
  235.                 G.DrawString(GetRGBFromColor(ColorC), Font, new SolidBrush(ColorD), new Rectangle(R3.X, R3.Height - 20, R3.Width, R3.Height), new StringFormat { Alignment = StringAlignment.Center });
  236.                 G.DrawString(GetRGBFromColor(ColorD), Font, new SolidBrush(ColorE), new Rectangle(R4.X, R4.Height - 20, R4.Width, R4.Height), new StringFormat { Alignment = StringAlignment.Center });
  237.                 G.DrawString(GetRGBFromColor(ColorE), Font, new SolidBrush(ColorD), new Rectangle(R5.X, R5.Height - 20, R5.Width, R5.Height), new StringFormat { Alignment = StringAlignment.Center });
  238.             }
  239.  
  240.             e.Graphics.DrawImage(B, 0, 0);
  241.             G.Dispose();
  242.             IMG = new Bitmap(B);
  243.             B.Dispose();
  244.  
  245.         }
  246.     }
  247.  
  248.     #endregion
  249.  
  250.     #region Helper MEthods
  251.  
  252.     public string GetRGBFromColor(Color C)
  253.     {
  254.         return String.Format("({0},{1},{2})", C.R, C.G, C.B);
  255.     }
  256.  
  257.     #endregion
  258.  
  259. }
  260.  
  261. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement