csharp Copy code
private enum ThemeStyle
{
Classic,
Vibrant,
Epic,
Neon,
Pastel,
DarkGlow,
Oceanic,
Sunset,
Cyberpunk,
Forest,
Retro,
Metallic,
Candy,
Space,
Vintage,
Aurora,
PrismBurst,
TechnoWave,
SolarFlare,
ElectricDream,
MysticRainbow,
TwilightPulse
}
private static int SafeRandomRange(Random rand, int minValue, int maxValue)
{
if (minValue > maxValue)
{
var temp = minValue;
minValue = maxValue;
maxValue = temp;
}
return rand.Next(minValue, maxValue + 1);
}
private Color RandomHueColor(int hMin, int hMax, double sMin, double sMax, double vMin, double vMax)
{
int hue = SafeRandomRange(_rand, hMin, hMax);
double saturation = _rand.NextDouble() * (sMax - sMin) + sMin;
double value = _rand.NextDouble() * (vMax - vMin) + vMin;
return ColorFromHSV(hue, saturation, value);
}
private List<Color> GenerateUniqueColorsDistinct(int count, double sMin, double sMax, double vMin, double vMax)
{
var colors = new List<Color>();
var usedHues = new HashSet<int>();
int attempts = 0;
int minHueDist = 360 / (count + 1);
int baseHue = _rand.Next(0, 360);
while (colors.Count < count && attempts < 10000)
{
int hue = (baseHue + colors.Count * minHueDist + _rand.Next(-minHueDist / 3, minHueDist / 3)) % 360;
if (usedHues.Any(h => Math.Abs(h - hue) < minHueDist / 2 || Math.Abs(h - hue) > 360 - minHueDist / 2))
{
attempts++;
baseHue = _rand.Next(0, 360);
continue;
}
usedHues.Add(hue);
double sat = _rand.NextDouble() * (sMax - sMin) + sMin;
double val = _rand.NextDouble() * (vMax - vMin) + vMin;
Color c = ColorFromHSV(hue, sat, val);
colors.Add(c);
attempts++;
}
// If not enough unique, fill with random colors anyway
while (colors.Count < count)
colors.Add(RandomHueColor(0, 360, sMin, sMax, vMin, vMax));
return colors;
}
private Color GenerateUniqueLightColorExcluding(List<Color> exclude)
{
int attempts = 0;
while (attempts < 1000)
{
double hue = _rand.NextDouble() * 360;
double saturation = _rand.NextDouble() * 0.3 + 0.2;
double value = _rand.NextDouble() * 0.4 + 0.6;
Color c = ColorFromHSV(hue, saturation, value);
bool isUnique = true;
foreach (var ex in exclude)
{
if (Math.Abs(ex.R - c.R) < 15 && Math.Abs(ex.G - c.G) < 15 && Math.Abs(ex.B - c.B) < 15)
{
isUnique = false;
break;
}
}
if (isUnique)
return c;
attempts++;
}
return Color.White;
}
private void GenerateSmartRandomTheme(bool reapply = false)
{
if (!reapply || _lastMainStart == Color.Empty)
{
var styles = Enum.GetValues(typeof(ThemeStyle)).Cast<ThemeStyle>().ToList();
// Shuffle styles for better random distribution
for (int i = styles.Count - 1; i > 0; i--)
{
int j = _rand.Next(i + 1);
(styles[i], styles[j]) = (styles[j], styles[i]);
}
var style = styles[0];
switch (style)
{
case ThemeStyle.Classic:
_lastMainStart = RandomHueColor(15, 45, 0.6, 0.8, 0.6, 0.9);
_lastMainEnd = RandomHueColor(35, 55, 0.7, 0.9, 0.7, 1.0);
_lastAccentsStart = RandomHueColor(150, 180, 0.5, 0.7, 0.6, 0.9);
_lastAccentsEnd = RandomHueColor(180, 210, 0.5, 0.7, 0.6, 0.9);
_lastTextColor = ColorFromHSV(0, 0, 0.1);
break;
case ThemeStyle.Vibrant:
{
var bases = GenerateUniqueColorsDistinct(5, 0.8, 1.0, 0.7, 0.9);
_lastMainStart = bases[0];
_lastMainEnd = bases[1];
_lastAccentsStart = bases[2];
_lastAccentsEnd = bases[3];
_lastTextColor = bases[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Epic:
_lastMainStart = RandomHueColor(0, 360, 0.75, 0.95, 0.1, 0.3);
_lastMainEnd = RandomHueColor(0, 360, 0.75, 0.95, 0.4, 0.6);
_lastAccentsStart = RandomHueColor(0, 360, 0.85, 1.0, 0.8, 1.0);
_lastAccentsEnd = RandomHueColor(0, 360, 0.65, 0.85, 0.6, 0.85);
_lastTextColor = Color.WhiteSmoke;
break;
case ThemeStyle.Neon:
_lastMainStart = Color.Black;
_lastMainEnd = ColorFromHSV(SafeRandomRange(_rand, 280, 320), 0.85, 0.3);
_lastAccentsStart = ColorFromHSV(SafeRandomRange(_rand, 150, 190), 0.95, 0.9);
_lastAccentsEnd = ColorFromHSV(SafeRandomRange(_rand, 200, 240), 0.9, 0.8);
_lastTextColor = Color.White;
break;
case ThemeStyle.Pastel:
{
var pastels = GenerateUniqueColorsDistinct(5, 0.25, 0.4, 0.85, 1.0);
_lastMainStart = pastels[0];
_lastMainEnd = pastels[1];
_lastAccentsStart = pastels[2];
_lastAccentsEnd = pastels[3];
_lastTextColor = pastels[4].GetBrightness() > 0.7 ? Color.FromArgb(255, 50, 50, 50) : Color.WhiteSmoke;
}
break;
case ThemeStyle.DarkGlow:
{
var darks = GenerateUniqueColorsDistinct(4, 0.7, 1.0, 0.05, 0.25);
_lastMainStart = darks[0];
_lastMainEnd = darks[1];
_lastAccentsStart = darks[2];
_lastAccentsEnd = darks[3];
_lastTextColor = GenerateUniqueLightColorExcluding(darks);
}
break;
case ThemeStyle.Oceanic:
{
var ocean = GenerateUniqueColorsDistinct(5, 0.6, 0.85, 0.4, 0.7);
for (int i = 0; i < ocean.Count; i++)
{
double h, s, v;
ColorToHSV(ocean[i], out h, out s, out v);
h = (h + 160) % 360;
ocean[i] = ColorFromHSV(h, s, v);
}
_lastMainStart = ocean[0];
_lastMainEnd = ocean[1];
_lastAccentsStart = ocean[2];
_lastAccentsEnd = ocean[3];
_lastTextColor = ocean[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Sunset:
{
var sunset = GenerateUniqueColorsDistinct(5, 0.6, 0.9, 0.4, 0.7);
for (int i = 0; i < sunset.Count; i++)
{
double h, s, v;
ColorToHSV(sunset[i], out h, out s, out v);
h = (h + 35) % 60 + 10;
sunset[i] = ColorFromHSV(h, s, v);
}
_lastMainStart = sunset[0];
_lastMainEnd = sunset[1];
_lastAccentsStart = sunset[2];
_lastAccentsEnd = sunset[3];
_lastTextColor = sunset[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Cyberpunk:
{
var cyber = GenerateUniqueColorsDistinct(5, 0.75, 1.0, 0.05, 0.35);
double[] cyberHueTargets = { 270, 300, 210, 240, 280 };
for (int i = 0; i < cyberHueTargets.Length; i++)
{
if(i < cyber.Count)
{
double h, s, v;
ColorToHSV(cyber[i], out h, out s, out v);
h = cyberHueTargets[i];
cyber[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = cyber[0];
_lastMainEnd = cyber[1];
_lastAccentsStart = cyber[2];
_lastAccentsEnd = cyber[3];
_lastTextColor = cyber[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Forest:
{
var forest = GenerateUniqueColorsDistinct(5, 0.5, 0.8, 0.3, 0.55);
for (int i = 0; i < forest.Count; i++)
{
double h, s, v;
ColorToHSV(forest[i], out h, out s, out v);
h = (h + 90) % 140 + 40;
forest[i] = ColorFromHSV(h, s, v);
}
_lastMainStart = forest[0];
_lastMainEnd = forest[1];
_lastAccentsStart = forest[2];
_lastAccentsEnd = forest[3];
_lastTextColor = forest[4].GetBrightness() > 0.7 ? Color.FromArgb(255, 20, 20, 20) : Color.White;
}
break;
case ThemeStyle.Retro:
{
var retro = GenerateUniqueColorsDistinct(5, 0.4, 0.7, 0.3, 0.6);
double[] retroHueTargets = { 290, 20, 40, 30, 350 };
for (int i = 0; i < retroHueTargets.Length; i++)
{
if(i < retro.Count)
{
double h, s, v;
ColorToHSV(retro[i], out h, out s, out v);
h = retroHueTargets[i];
retro[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = retro[0];
_lastMainEnd = retro[1];
_lastAccentsStart = retro[2];
_lastAccentsEnd = retro[3];
_lastTextColor = retro[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Metallic:
{
var metallic = GenerateUniqueColorsDistinct(5, 0.05, 0.15, 0.3, 0.55);
for (int i = 0; i < metallic.Count; i++)
{
double h, s, v;
ColorToHSV(metallic[i], out h, out s, out v);
s *= 0.1;
metallic[i] = ColorFromHSV(h, s, v);
}
_lastMainStart = metallic[0];
_lastMainEnd = metallic[1];
_lastAccentsStart = metallic[2];
_lastAccentsEnd = metallic[3];
_lastTextColor = metallic[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Candy:
{
var candy = GenerateUniqueColorsDistinct(5, 0.7, 0.95, 0.7, 1.0);
double[] candyHueTargets = { 320, 350, 330, 310, 300 };
for (int i = 0; i < candyHueTargets.Length; i++)
{
if(i < candy.Count)
{
double h, s, v;
ColorToHSV(candy[i], out h, out s, out v);
h = candyHueTargets[i];
candy[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = candy[0];
_lastMainEnd = candy[1];
_lastAccentsStart = candy[2];
_lastAccentsEnd = candy[3];
_lastTextColor = candy[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Space:
{
var space = GenerateUniqueColorsDistinct(5, 0.7, 0.9, 0.05, 0.35);
double[] spaceHueTargets = { 240, 260, 250, 220, 280 };
for (int i = 0; i < spaceHueTargets.Length; i++)
{
if(i < space.Count)
{
double h, s, v;
ColorToHSV(space[i], out h, out s, out v);
h = spaceHueTargets[i];
space[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = space[0];
_lastMainEnd = space[1];
_lastAccentsStart = space[2];
_lastAccentsEnd = space[3];
_lastTextColor = space[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Vintage:
{
var vintage = GenerateUniqueColorsDistinct(5, 0.4, 0.65, 0.3, 0.55);
double[] vintageHueTargets = { 40, 50, 30, 20, 10 };
for (int i = 0; i < vintageHueTargets.Length; i++)
{
if (i < vintage.Count)
{
double h, s, v;
ColorToHSV(vintage[i], out h, out s, out v);
h = vintageHueTargets[i];
vintage[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = vintage[0];
_lastMainEnd = vintage[1];
_lastAccentsStart = vintage[2];
_lastAccentsEnd = vintage[3];
_lastTextColor = vintage[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.Aurora:
{
var aurora = GenerateUniqueColorsDistinct(5, 0.7, 1.0, 0.25, 0.55);
double[] auroraHueTargets = { 150, 180, 130, 220, 200 };
for (int i = 0; i < auroraHueTargets.Length; i++)
{
if (i < aurora.Count)
{
double h, s, v;
ColorToHSV(aurora[i], out h, out s, out v);
h = auroraHueTargets[i];
aurora[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = aurora[0];
_lastMainEnd = aurora[1];
_lastAccentsStart = aurora[2];
_lastAccentsEnd = aurora[3];
_lastTextColor = aurora[4].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.PrismBurst:
{
var baseColors = GenerateUniqueColorsDistinct(5, 0.8, 1.0, 0.7, 1.0);
_lastMainStart = baseColors[0];
_lastMainEnd = baseColors[1];
_lastAccentsStart = baseColors[2];
_lastAccentsEnd = baseColors[3];
_lastTextColor = baseColors[4].GetBrightness() < 0.4 ? Color.White : Color.Black;
}
break;
case ThemeStyle.TechnoWave:
{
_lastMainStart = ColorFromHSV(_rand.Next(200, 240), 0.95, 0.1);
_lastMainEnd = ColorFromHSV(_rand.Next(220, 260), 0.95, 0.3);
_lastAccentsStart = ColorFromHSV(_rand.Next(280, 320), 0.9, 0.8);
_lastAccentsEnd = ColorFromHSV(_rand.Next(300, 340), 0.9, 0.9);
_lastTextColor = Color.White;
}
break;
case ThemeStyle.SolarFlare:
{
_lastMainStart = ColorFromHSV(_rand.Next(10, 40), 0.9, 0.3);
_lastMainEnd = ColorFromHSV(_rand.Next(20, 50), 0.9, 0.5);
_lastAccentsStart = ColorFromHSV(_rand.Next(30, 60), 1.0, 0.95);
_lastAccentsEnd = ColorFromHSV(_rand.Next(15, 45), 1.0, 0.85);
_lastTextColor = Color.FromArgb(255, 30, 30, 30);
}
break;
case ThemeStyle.ElectricDream:
{
var colors = GenerateUniqueColorsDistinct(5, 0.5, 0.85, 0.6, 0.85);
double[] targetHues = { 280, 190, 320, 350, 300 };
for (int i = 0; i < targetHues.Length; i++)
{
if (i < colors.Count)
{
double h, s, v;
ColorToHSV(colors[i], out h, out s, out v);
h = targetHues[i];
colors[i] = ColorFromHSV(h, s, v);
}
}
_lastMainStart = colors[0];
_lastMainEnd = colors[1];
_lastAccentsStart = colors[2];
_lastAccentsEnd = colors[3];
_lastTextColor = colors[4].GetBrightness() < 0.5 ? Color.WhiteSmoke : Color.Black;
}
break;
case ThemeStyle.MysticRainbow:
{
_lastMainStart = ColorFromHSV(_rand.Next(0, 360), 0.2, 0.15);
_lastMainEnd = ColorFromHSV((_rand.Next(0, 360) + 180) % 360, 0.3, 0.25);
var accents = GenerateUniqueColorsDistinct(3, 0.85, 1.0, 0.8, 1.0);
_lastAccentsStart = accents[0];
_lastAccentsEnd = accents[1];
_lastTextColor = accents[2].GetBrightness() < 0.5 ? Color.White : Color.Black;
}
break;
case ThemeStyle.TwilightPulse:
{
_lastMainStart = ColorFromHSV(_rand.Next(250, 280), 0.6, 0.45);
_lastMainEnd = ColorFromHSV(_rand.Next(230, 260), 0.65, 0.6);
_lastAccentsStart = ColorFromHSV(_rand.Next(310, 350), 0.9, 0.95);
_lastAccentsEnd = ColorFromHSV(_rand.Next(270, 310), 0.9, 0.8);
_lastTextColor = Color.FromArgb(255, 240, 240, 250);
}
break;
}
}
Theme(_lastMainStart, _lastMainEnd, _lastAccentsStart, _lastAccentsEnd, _lastTextColor);
}