THE_LORD

iPalette ~ GDI Color Palette Creater

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