Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace EasyButton {
- public static class ColorEditor {
- public static int DecreaseArgbValue(int argbValue, int intensity) {
- intensity = Math.Abs(intensity) * -1;
- return GetArgbValue(argbValue + intensity);
- }
- public static int GetArgbValue(int argbValue) {
- argbValue = Math.Abs(argbValue);
- if (argbValue < 0) {
- return 0;
- } else if (argbValue > 255) {
- return 255;
- } else {
- return argbValue;
- }
- }
- public static float GetPercentage(float percentage) {
- if (percentage < 0) {
- return 0;
- } else if (percentage > 1) {
- return 1;
- } else {
- return percentage;
- }
- }
- public static Color GetGrayscale(int argbValue) {
- return GetGrayscale(255, argbValue);
- }
- public static Color GetGrayscale(int alpha, int argbValue) {
- argbValue = GetArgbValue(argbValue);
- return Color.FromArgb(alpha, argbValue, argbValue, argbValue);
- }
- public static int IncreaseArgbValue(int argbValue, int intensity) {
- intensity = Math.Abs(intensity);
- return GetArgbValue(argbValue + intensity);
- }
- public static Color Intensify(Color originalColor, float percentage) {
- percentage = GetPercentage(Math.Abs(percentage));
- int r, g, b;
- r = originalColor.R + (int)(originalColor.R * percentage);
- g = originalColor.G + (int)(originalColor.G * percentage);
- b = originalColor.B + (int)(originalColor.B * percentage);
- return Color.FromArgb(originalColor.A, r, g, b);
- }
- public static Color Intensify(Color originalColor, int intensity) {
- intensity = Math.Abs(intensity);
- int r, g, b;
- r = IncreaseArgbValue(originalColor.R, intensity);
- g = IncreaseArgbValue(originalColor.G, intensity);
- b = IncreaseArgbValue(originalColor.B, intensity);
- return Color.FromArgb(originalColor.A, r, g, b);
- }
- public static Color Reduce(Color originalColor, float percentage) {
- percentage = GetPercentage(Math.Abs(percentage));
- int r, g, b;
- r = originalColor.R - (int)(originalColor.R * percentage);
- g = originalColor.G - (int)(originalColor.G * percentage);
- b = originalColor.B - (int)(originalColor.B * percentage);
- return Color.FromArgb(originalColor.A, r, g, b);
- }
- public static Color Reduce(Color originalColor, int intensity) {
- intensity = Math.Abs(intensity);
- int r, g, b;
- r = DecreaseArgbValue(originalColor.R, intensity);
- g = DecreaseArgbValue(originalColor.G, intensity);
- b = DecreaseArgbValue(originalColor.B, intensity);
- return Color.FromArgb(originalColor.A, r, g, b);
- }
- }
- public sealed class PopButton : Control {
- private sealed class GradientEllipse {
- public float Angle { get; set; }
- public Color BaseColor { get; set; }
- public Rectangle ClientRectangle { get; set; }
- public Rectangle DrawRectangle { get; set; }
- public LinearGradientBrush GradientBrush {
- get {
- return new LinearGradientBrush(ClientRectangle, ColorEditor.Intensify(BaseColor, GradientDifference), ColorEditor.Reduce(BaseColor, GradientDifference), Angle);
- }
- }
- public int GradientDifference { get; set; }
- public Pen GradientPen {
- get {
- return new Pen(GradientBrush, PenWidth);
- }
- }
- public int PenWidth { get; set; }
- public GradientEllipse() {
- Angle = 90.0f;
- BaseColor = Color.Black;
- ClientRectangle = new Rectangle(0, 0, 1, 1);
- DrawRectangle = new Rectangle(0, 0, 1, 1);
- GradientDifference = 0;
- PenWidth = 1;
- }
- }
- GradientEllipse oEllipse, iEllipse;
- public int BorderWidth {
- get {
- return oEllipse.PenWidth;
- }
- set {
- if (value < 5) {
- oEllipse.PenWidth = 5;
- } else if (value > 40) {
- oEllipse.PenWidth = 40;
- } else {
- oEllipse.PenWidth = value;
- }
- SetGradientEllipses();
- }
- }
- public PopButton() {
- SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
- ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);
- oEllipse = new GradientEllipse();
- oEllipse.BaseColor = Color.FromArgb(130, 130, 130);
- oEllipse.GradientDifference = 70;
- oEllipse.PenWidth = 25;
- iEllipse = new GradientEllipse();
- iEllipse.Angle = -90.0f;
- iEllipse.BaseColor = Color.FromArgb(120, 120, 120);
- iEllipse.GradientDifference = 80;
- iEllipse.PenWidth = 3;
- BackColor = Color.FromArgb(35, 125, 220);
- Size = new Size(200, 200);
- }
- protected override void OnPaint(PaintEventArgs e) {
- e.Graphics.Clear(Parent.BackColor);
- e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
- using (SolidBrush backBrush = new SolidBrush(BackColor)) {
- e.Graphics.FillEllipse(backBrush, oEllipse.DrawRectangle);
- }
- using (GraphicsPath shadowPath = new GraphicsPath()) {
- shadowPath.AddEllipse(iEllipse.DrawRectangle);
- using (PathGradientBrush shadowBrush = new PathGradientBrush(shadowPath)) {
- ColorBlend shadowBlend = new ColorBlend(4);
- shadowBlend.Colors = new Color[4] {
- Color.FromArgb(200, Color.Black),
- Color.FromArgb(120, Color.Black),
- Color.Transparent,
- Color.Transparent };
- shadowBlend.Positions = new float[4] { 0.0f, 0.12f, 0.25f, 1.0f };
- shadowBrush.InterpolationColors = shadowBlend;
- e.Graphics.FillEllipse(shadowBrush, iEllipse.DrawRectangle);
- }
- }
- using (GraphicsPath effectPath = new GraphicsPath()) {
- effectPath.AddEllipse(oEllipse.ClientRectangle);
- using (PathGradientBrush lowEffectBrush = new PathGradientBrush(effectPath)) {
- lowEffectBrush.CenterPoint = new Point(
- iEllipse.DrawRectangle.X + (iEllipse.DrawRectangle.Width / 2),
- iEllipse.DrawRectangle.Y + (iEllipse.DrawRectangle.Height - (Height / 7)));
- lowEffectBrush.CenterColor = Color.FromArgb(180, Color.White);
- lowEffectBrush.SurroundColors = new Color[1] { Color.Transparent };
- e.Graphics.FillEllipse(lowEffectBrush, iEllipse.DrawRectangle);
- }
- }
- e.Graphics.DrawEllipse(oEllipse.GradientPen, oEllipse.DrawRectangle);
- e.Graphics.DrawEllipse(iEllipse.GradientPen, iEllipse.DrawRectangle);
- }
- protected override void OnSizeChanged(EventArgs e) {
- SetGradientEllipses();
- }
- public void SetGradientEllipses() {
- oEllipse.ClientRectangle = new Rectangle(0, 0, Width - 1, Height - 1);
- oEllipse.DrawRectangle = new Rectangle(
- oEllipse.PenWidth / 2,
- oEllipse.PenWidth / 2,
- Width - oEllipse.PenWidth - 1,
- Height - oEllipse.PenWidth - 1);
- iEllipse.ClientRectangle = oEllipse.DrawRectangle;
- iEllipse.DrawRectangle = new Rectangle(
- oEllipse.PenWidth - (iEllipse.PenWidth / 2),
- oEllipse.PenWidth - (iEllipse.PenWidth / 2),
- Width - (oEllipse.PenWidth * 2),
- Height - (oEllipse.PenWidth * 2));
- Invalidate();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment