Computers && Coding == Art

PC Affinity [email protected]
Menu
  • Home
  • Downloads
  • Projects
  • About Me

ColorFadeCalculator

Download latest version from Github

Generate custom color steps between two colors.

Simple, but very useful.


Need more pizazz in your application? I got you. Want your application to pop? I got you. Try this easy to use ColorFadeCalculator resource. It’ll change your life.


Settings


  • Variables
    • Steps = Contains all colors between two colors. (read – List<Color>)

Functions


ColorFadeCalculator()

ColorFadeCalculator(Color fromColor, Color toColor)

ColorFadeCalculator(Color fromColor, Color toColor, int targetSteps = 60)

ColorFadeCalculator(Color fromColor, Color toColor, int targetSteps = 60, bool transparentFading = false)

ColorFadeCalculator(Color fromColor, Color toColor, int targetSteps = 60, int accuracy = 1)

GetMidColor(Color color1, Color color2)
  • ColorFadeCalculator
    • fromColor = First Color. (Color)
    • toColor = Second Color. (Color)
    • targetSteps = How many steps between fromColor and toColor. (int)
      • This will determine the final size of Steps List variable.
    • accuracy = Skip processing color steps if delay is an issue. (bool)
      • 1 will process every color between fromColor and toColor.
      • 2 will process every other color between fromColor and toColor.
      • etc.
    • transparentFading = Include Alpha channel when fading. (int)

  • GetMidColor
    • color1 = First Color. (Color)
    • color2 = Second Color. (Color)

Simple Example:

label.Backcolor = Color.Red;
ColorFadeCalculator cfc = ColorFadeCalculator(Color.Red, Color.Blue);

for (int i = 0; i < cfc.Steps.Count; i++)
{
    label.Backcolor = cfc.Steps[i];
}

Fading Label Example:

const int steps = 100;
const int delay = 5;
const int timeout = 2000;
Color labelFore = label1.ForeColor;
Color labelBack = label1.BackColor;
Color formBack = this.BackColor;

// Create a ColorFadeCalculator which holds all of your Color Steps.
ColorFadeCalculator labelFade = new ColorFadeCalculator(labelFore, labelBack, steps);

label1.text = "Original text."

Stopwatch sw = new Stopwatch();
sw.Start();

// Fade OUT here
for (int i = 0; i < labelFade.Steps.Count; i++)
{
	label1.ForeColor = labelFade.Steps[i];
	label1.Update();

	// I use a delay to extend the fading time without extending the Steps.
	if (delay > 0)
	{
		long check = sw.ElapsedMilliseconds;
		while (sw.ElapsedMilliseconds < check + delay)
		{
			Thread.Sleep(0);
		}
	}
	
	// I use a timeout just in case the PC is running slowly.
	if (sw.ElapsedMilliseconds >= timeout)
	{
		label1.ForeColor = labelFade.Steps[labelFade.Steps.Count - 1];
                label1.Update();
		break;
	}
}

//Here we change what we need to after everything is Faded Out.
label1.Text = "New label text.";

sw.Restart();

// And now we Fade In the same way we Faded Out.
// But counting in reverse.
for (int i = labelFade.Steps.Count - 1; i >= 0; i--)
{
	label1.ForeColor = labelFade.Steps[i];
        label1.Update();
	
	// I use a delay to extend the fading time without extending the Steps
	if (delay > 0)
	{
		long check = sw.ElapsedMilliseconds;
		while (sw.ElapsedMilliseconds < check + delay)
		{
			Thread.Sleep(0);
		}
	}
	
	// I use a timeout just in case the PC is running slowly.
	if (sw.ElapsedMilliseconds >= timeout)
	{
		label1.ForeColor = labelFade.Steps[0];
                label1.Update();
		break;
	}
}

Fading PictureBox Example:

const int steps = 100;
const int delay = 5;
const int timeout = 2000;
Color formBack = this.BackColor;

//For PictureBox Images, you will need to set the Transparencey option to True.
//The fromColor can be set to anything Transparent.
//If fromColor is set to Color.Transparent, you may notice a dark shade when fading.
//Try to use a Transparent color of the Parent for better fading affect!
ColorFadeCalculator photoFade = new ColorFadeCalculator(Color.FromArgb(0, formBack.R, formBack.G, formBack.B), formBack, steps, true);

Image photoImage = (Image)pictureBox1.Image.Clone();

Stopwatch sw = new Stopwatch();
sw.Start();

// Fade OUT here
for (int i = 0; i < photoFade.Steps.Count; i++)
{
	if (photoImage != null)
	{
		using Image img = (Bitmap)photoImage.Clone();
		using Graphics g = Graphics.FromImage(img);
		using SolidBrush myBrush = new SolidBrush(photoFade.Steps[i]);
		g.FillRectangle(myBrush, 0, 0, img.Width, img.Height);
		g.Save();
		UpdatePhoto(img);
	}

	pictureBox1.Update();
	
	// I use a delay to extend the fading time without extending the Steps.
	if (delay > 0)
	{
		long check = sw.ElapsedMilliseconds;
		while (sw.ElapsedMilliseconds < check + delay)
		{
			Thread.Sleep(0);
		}
	}
	
	// I use a timeout just in case the PC is running slowly.
	if (sw.ElapsedMilliseconds >= timeout)
	{
		if (photoImage != null)
		{
			using Image img = (Bitmap)photoImage.Clone();
			using Graphics g = Graphics.FromImage(img);
			using SolidBrush myBrush = new SolidBrush(photoFade.Steps[photoFade.Steps.Count - 1]);
			g.FillRectangle(myBrush, 0, 0, img.Width, img.Height);
			g.Save();
			UpdatePhoto(img);
		}

		pictureBox1.Update();
		break;
	}
}



Please feel free to view the source code over at GitHub. Completely available. Let me know if you have any questions or concerns. And please share with your friends!



Share this:

  • Twitter
  • Facebook
  • SMS
  • Email

Like this:

Like Loading...

Leave a Reply Cancel reply

My LinkedIn Profile


'Derrick Ducote'

Tags

API ASP.NET C# Clipboard Cycler Cloudflare Coding ColorFadeCalculator ConsoleWriter DLL Downloads First FMOL Assistant FMOL Marquee FP Time Tracker Gaming Hotkey Commands HTML Learning MagicHat MagicWheel Mouse Commands NGINX Other Personal PHP Picket Projects RegistryCommands RoundedButtons Server Site Information Synology Toolkit Cleanup

Categories

  • Apps
  • Coding
  • Configurations
  • Other
  • PHP
  • Server
  • Site Information

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

PC Affinity 2023 . Powered by WordPress

%d bloggers like this: