Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/colors/csharp$ _

Generate Random Colors in C# - Hex, RGB & Color Objects

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C#

What: Generate random colors in C# using hex strings, RGB values, and System.Drawing.Color objects for WPF, WinForms, and ASP.NET applications.

Try it: Use our interactive Colors generator or integrate this code into your C# application.

Generate random colors in C# using hex strings, RGB values, and System.Drawing.Color objects for WPF, WinForms, and ASP.NET applications. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C++ , Ruby and Go or use our interactive web generator.

C# Code Example

using System;
using System.Drawing;

class RandomColorGenerator
{
    private static readonly Random random = new Random();

    static void Main()
    {
        // Generate Random Hex Color
        string hexColor = RandomHexColor();
        Console.WriteLine($"Hex Color: {hexColor}");

        // Generate Random RGB Color String
        string rgbColor = RandomRgbColor();
        Console.WriteLine($"RGB Color: {rgbColor}");

        // Generate Random System.Drawing.Color
        Color color = RandomColor();
        Console.WriteLine($"Color Object: {ColorToRgbString(color)}");

        // Generate Random HSL Color
        string hslColor = RandomHslColor();
        Console.WriteLine($"HSL Color: {hslColor}");

        // Generate Pastel Color
        string pastelColor = RandomPastelColor();
        Console.WriteLine($"Pastel Color: {pastelColor}");
    }

    public static string RandomHexColor()
    {
        int rgb = random.Next(0x1000000);
        return $"#{rgb:X6}";
    }

    public static string RandomRgbColor()
    {
        int r = random.Next(256);
        int g = random.Next(256);
        int b = random.Next(256);
        return $"rgb({r}, {g}, {b})";
    }

    public static Color RandomColor()
    {
        return Color.FromArgb(
            random.Next(256),
            random.Next(256),
            random.Next(256)
        );
    }

    public static string RandomHslColor()
    {
        int h = random.Next(361);
        int s = random.Next(101);
        int l = random.Next(101);
        return $"hsl({h}, {s}%, {l}%)";
    }

    public static string RandomPastelColor()
    {
        int h = random.Next(361);
        int s = random.Next(25, 101);
        int l = random.Next(70, 91);
        return $"hsl({h}, {s}%, {l}%)";
    }

    public static string ColorToRgbString(Color color)
    {
        return $"rgb({color.R}, {color.G}, {color.B})";
    }
}

[EXPLANATION]

C# uses System.Random for generating random values. For hex colors, use random.Next(0x1000000) to generate values 0-16777215 and format with :X6 to create 6-digit hex. The System.Drawing.Color struct is created with Color.FromArgb() accepting RGB values (0-255). HSL colors are formatted as strings since C# doesn't have built-in HSL support. Use random.Next(256) for RGB channels (0-255 exclusive upper bound). String interpolation with $"" provides clean formatting.

Expected Output

Hex Color: #7A3E9D
RGB Color: rgb(198, 112, 67)
Color Object: rgb(142, 201, 89)
HSL Color: hsl(204, 78%, 56%)
Pastel Color: hsl(312, 68%, 79%)

Common Use Cases

  • Generate colors for WPF UI elements
  • Create color schemes for WinForms applications
  • Assign colors to chart series in ASP.NET
  • Generate random backgrounds for controls
  • Create color palettes for data visualization

Important Notes

  • System.Drawing.Color is in System.Drawing assembly
  • random.Next(n) is exclusive upper bound (0 to n-1)
  • WPF uses System.Windows.Media.Color instead
  • For thread-safety, use ThreadLocal<Random>
  • Consider Color.FromKnownColor() for predefined colors

Try Our Interactive Generator

Don't want to write code? Use our free web-based Colors generator with instant results.

TRY COLORS GENERATOR →