0Hawk0

3D Pop Button

Mar 10th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.95 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5.  
  6. namespace EasyButton {
  7.  
  8.     public static class ColorEditor {
  9.  
  10.         public static int DecreaseArgbValue(int argbValue, int intensity) {
  11.  
  12.             intensity = Math.Abs(intensity) * -1;
  13.             return GetArgbValue(argbValue + intensity);
  14.  
  15.         }
  16.  
  17.         public static int GetArgbValue(int argbValue) {
  18.  
  19.             argbValue = Math.Abs(argbValue);
  20.  
  21.             if (argbValue < 0) {
  22.                 return 0;
  23.             } else if (argbValue > 255) {
  24.                 return 255;
  25.             } else {
  26.                 return argbValue;
  27.             }
  28.  
  29.         }
  30.  
  31.         public static float GetPercentage(float percentage) {
  32.  
  33.             if (percentage < 0) {
  34.                 return 0;
  35.             } else if (percentage > 1) {
  36.                 return 1;
  37.             } else {
  38.                 return percentage;
  39.             }
  40.  
  41.         }
  42.  
  43.         public static Color GetGrayscale(int argbValue) {
  44.  
  45.             return GetGrayscale(255, argbValue);
  46.  
  47.         }
  48.  
  49.         public static Color GetGrayscale(int alpha, int argbValue) {
  50.            
  51.             argbValue = GetArgbValue(argbValue);
  52.             return Color.FromArgb(alpha, argbValue, argbValue, argbValue);
  53.  
  54.         }
  55.  
  56.         public static int IncreaseArgbValue(int argbValue, int intensity) {
  57.  
  58.             intensity = Math.Abs(intensity);
  59.             return GetArgbValue(argbValue + intensity);
  60.  
  61.         }
  62.  
  63.         public static Color Intensify(Color originalColor, float percentage) {
  64.  
  65.             percentage = GetPercentage(Math.Abs(percentage));
  66.  
  67.             int r, g, b;
  68.  
  69.             r = originalColor.R + (int)(originalColor.R * percentage);
  70.             g = originalColor.G + (int)(originalColor.G * percentage);
  71.             b = originalColor.B + (int)(originalColor.B * percentage);
  72.  
  73.             return Color.FromArgb(originalColor.A, r, g, b);
  74.  
  75.         }
  76.  
  77.         public static Color Intensify(Color originalColor, int intensity) {
  78.  
  79.             intensity = Math.Abs(intensity);
  80.  
  81.             int r, g, b;
  82.  
  83.             r = IncreaseArgbValue(originalColor.R, intensity);
  84.             g = IncreaseArgbValue(originalColor.G, intensity);
  85.             b = IncreaseArgbValue(originalColor.B, intensity);
  86.  
  87.             return Color.FromArgb(originalColor.A, r, g, b);
  88.  
  89.         }
  90.  
  91.         public static Color Reduce(Color originalColor, float percentage) {
  92.  
  93.             percentage = GetPercentage(Math.Abs(percentage));
  94.  
  95.             int r, g, b;
  96.  
  97.             r = originalColor.R - (int)(originalColor.R * percentage);
  98.             g = originalColor.G - (int)(originalColor.G * percentage);
  99.             b = originalColor.B - (int)(originalColor.B * percentage);
  100.  
  101.             return Color.FromArgb(originalColor.A, r, g, b);
  102.  
  103.         }
  104.  
  105.         public static Color Reduce(Color originalColor, int intensity) {
  106.  
  107.             intensity = Math.Abs(intensity);
  108.  
  109.             int r, g, b;
  110.  
  111.             r = DecreaseArgbValue(originalColor.R, intensity);
  112.             g = DecreaseArgbValue(originalColor.G, intensity);
  113.             b = DecreaseArgbValue(originalColor.B, intensity);
  114.  
  115.             return Color.FromArgb(originalColor.A, r, g, b);
  116.  
  117.         }
  118.  
  119.     }
  120.  
  121.     public sealed class PopButton : Control {
  122.  
  123.         private sealed class GradientEllipse {
  124.  
  125.             public float Angle { get; set; }
  126.  
  127.             public Color BaseColor { get; set; }
  128.  
  129.             public Rectangle ClientRectangle { get; set; }
  130.  
  131.             public Rectangle DrawRectangle { get; set; }
  132.  
  133.             public LinearGradientBrush GradientBrush {
  134.                 get {
  135.                     return new LinearGradientBrush(ClientRectangle, ColorEditor.Intensify(BaseColor, GradientDifference), ColorEditor.Reduce(BaseColor, GradientDifference), Angle);
  136.                 }
  137.             }
  138.  
  139.             public int GradientDifference { get; set; }
  140.  
  141.             public Pen GradientPen {
  142.                 get {
  143.                     return new Pen(GradientBrush, PenWidth);
  144.                 }
  145.             }
  146.  
  147.             public int PenWidth { get; set; }
  148.  
  149.             public GradientEllipse() {
  150.                 Angle = 90.0f;
  151.                 BaseColor = Color.Black;
  152.                 ClientRectangle = new Rectangle(0, 0, 1, 1);
  153.                 DrawRectangle = new Rectangle(0, 0, 1, 1);
  154.                 GradientDifference = 0;
  155.                 PenWidth = 1;
  156.             }
  157.  
  158.         }
  159.  
  160.         GradientEllipse oEllipse, iEllipse;
  161.  
  162.         public int BorderWidth {
  163.             get {
  164.                 return oEllipse.PenWidth;
  165.             }
  166.             set {
  167.                 if (value < 5) {
  168.                     oEllipse.PenWidth = 5;
  169.                 } else if (value > 40) {
  170.                     oEllipse.PenWidth = 40;
  171.                 } else {
  172.                     oEllipse.PenWidth = value;
  173.                 }
  174.                 SetGradientEllipses();
  175.             }
  176.         }
  177.  
  178.         public PopButton() {
  179.  
  180.             SetStyle( ControlStyles.AllPaintingInWmPaint |  ControlStyles.OptimizedDoubleBuffer |
  181.                 ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
  182.  
  183.             oEllipse = new GradientEllipse();
  184.             oEllipse.BaseColor = Color.FromArgb(130, 130, 130);
  185.             oEllipse.GradientDifference = 70;
  186.             oEllipse.PenWidth = 25;
  187.  
  188.             iEllipse = new GradientEllipse();
  189.             iEllipse.Angle = -90.0f;
  190.             iEllipse.BaseColor = Color.FromArgb(120, 120, 120);
  191.             iEllipse.GradientDifference = 80;
  192.             iEllipse.PenWidth = 3;
  193.  
  194.             BackColor = Color.FromArgb(35, 125, 220);
  195.             Size = new Size(200, 200);
  196.  
  197.         }
  198.  
  199.         protected override void OnPaint(PaintEventArgs e) {
  200.  
  201.             e.Graphics.Clear(Parent.BackColor);
  202.             e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  203.  
  204.             using (SolidBrush backBrush = new SolidBrush(BackColor)) {
  205.                 e.Graphics.FillEllipse(backBrush, oEllipse.DrawRectangle);
  206.             }
  207.  
  208.             using (GraphicsPath shadowPath = new GraphicsPath()) {
  209.  
  210.                 shadowPath.AddEllipse(iEllipse.DrawRectangle);
  211.  
  212.                 using (PathGradientBrush shadowBrush = new PathGradientBrush(shadowPath)) {
  213.  
  214.                     ColorBlend shadowBlend = new ColorBlend(4);
  215.  
  216.                     shadowBlend.Colors = new Color[4] {
  217.                         Color.FromArgb(200, Color.Black),
  218.                         Color.FromArgb(120, Color.Black),
  219.                         Color.Transparent,
  220.                         Color.Transparent };
  221.  
  222.                     shadowBlend.Positions = new float[4] { 0.0f, 0.12f, 0.25f, 1.0f };
  223.  
  224.                     shadowBrush.InterpolationColors = shadowBlend;
  225.  
  226.                     e.Graphics.FillEllipse(shadowBrush, iEllipse.DrawRectangle);
  227.  
  228.                 }
  229.  
  230.             }
  231.  
  232.             using (GraphicsPath effectPath = new GraphicsPath()) {
  233.  
  234.                 effectPath.AddEllipse(oEllipse.ClientRectangle);
  235.  
  236.                 using (PathGradientBrush lowEffectBrush = new PathGradientBrush(effectPath)) {
  237.  
  238.                     lowEffectBrush.CenterPoint = new Point(
  239.                         iEllipse.DrawRectangle.X + (iEllipse.DrawRectangle.Width / 2),
  240.                         iEllipse.DrawRectangle.Y + (iEllipse.DrawRectangle.Height - (Height / 7)));
  241.  
  242.                     lowEffectBrush.CenterColor = Color.FromArgb(180, Color.White);
  243.                     lowEffectBrush.SurroundColors = new Color[1] { Color.Transparent };
  244.  
  245.                     e.Graphics.FillEllipse(lowEffectBrush, iEllipse.DrawRectangle);
  246.  
  247.                 }
  248.  
  249.             }
  250.  
  251.             e.Graphics.DrawEllipse(oEllipse.GradientPen, oEllipse.DrawRectangle);
  252.  
  253.             e.Graphics.DrawEllipse(iEllipse.GradientPen, iEllipse.DrawRectangle);
  254.  
  255.         }
  256.  
  257.         protected override void OnSizeChanged(EventArgs e) {
  258.  
  259.             SetGradientEllipses();
  260.  
  261.         }
  262.  
  263.         public void SetGradientEllipses() {
  264.  
  265.             oEllipse.ClientRectangle = new Rectangle(0, 0, Width - 1, Height - 1);
  266.  
  267.             oEllipse.DrawRectangle = new Rectangle(
  268.                 oEllipse.PenWidth / 2,
  269.                 oEllipse.PenWidth / 2,
  270.                 Width - oEllipse.PenWidth - 1,
  271.                 Height - oEllipse.PenWidth - 1);
  272.  
  273.             iEllipse.ClientRectangle = oEllipse.DrawRectangle;
  274.  
  275.             iEllipse.DrawRectangle = new Rectangle(
  276.                 oEllipse.PenWidth - (iEllipse.PenWidth / 2),
  277.                 oEllipse.PenWidth - (iEllipse.PenWidth / 2),
  278.                 Width - (oEllipse.PenWidth * 2),
  279.                 Height - (oEllipse.PenWidth * 2));
  280.  
  281.             Invalidate();
  282.  
  283.         }
  284.  
  285.     }
  286.  
  287. }
Advertisement
Add Comment
Please, Sign In to add comment