Skip to content

Instantly share code, notes, and snippets.

@AmirMahdyJebreily
Created May 24, 2024 13:23
Show Gist options
  • Save AmirMahdyJebreily/5174fdceec597c1893866cfc0c92f54d to your computer and use it in GitHub Desktop.
Save AmirMahdyJebreily/5174fdceec597c1893866cfc0c92f54d to your computer and use it in GitHub Desktop.
Random colors for Windows Form C#
using System;
using System.Collections.Generic;
using System.Drawing;
namespace CodeAgha
{
public static class ThemeColor
{
public static Color PrimaryColor { get; set; }
public static Color SecondaryColor { get; set; }
public static List<string> ColorList = new List<string>() { "#3F51B5",
"#009688",
"#FF5722",
"#607D8B",
"#FF9800",
"#9C27B0",
"#2196F3",
"#EA676C",
"#E41A4A",
"#5978BB",
"#018790",
"#0E3441",
"#00B0AD",
"#721D47",
"#EA4833",
"#EF937E",
"#F37521",
"#A12059",
"#126881",
"#8BC240",
"#364D5B",
"#C7DC5B",
"#0094BC",
"#E4126B",
"#43B76E",
"#7BCFE9",
"#B71C46"};
public static Color ChangeColorBrightness(Color color, double correctionFactor)
{
double red = color.R;
double green = color.G;
double blue = color.B;
//If correction factor is less than 0, darken color.
if (correctionFactor < 0)
{
correctionFactor = 1 + correctionFactor;
red *= correctionFactor;
green *= correctionFactor;
blue *= correctionFactor;
}
//If correction factor is greater than zero, lighten color.
else
{
red = (255 - red) * correctionFactor + red;
green = (255 - green) * correctionFactor + green;
blue = (255 - blue) * correctionFactor + blue;
}
return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
}
public static Color SelectThemeColor()
{
Random random = new Random();
int tempIndex = 0;
int index = random.Next(ThemeColor.ColorList.Count);
while (tempIndex == index)
{
index = random.Next(ThemeColor.ColorList.Count);
}
tempIndex = index;
string color = ThemeColor.ColorList[index];
return ColorTranslator.FromHtml(color);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment