C# Miscellaneous Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use a list of color names and the Random
class to generate random color names in C#. Here is an example:
List<string> colorNames = new List<string> { "Red", "Green", "Blue", "Yellow", "Orange", "Purple" }; Random random = new Random(); string randomColorName = colorNames[random.Next(colorNames.Count)]; Console.WriteLine("Random color name: " + randomColorName);
This will output a random color name from the list.
You can use the ColorConverter
class to convert a string to a Color
in C#. Here is an example:
string colorString = "Red"; Color color = (Color)new ColorConverter().ConvertFromString(colorString);
This will convert the string "Red"
to a Color
with the value Color.Red
.
You can use the ColorTranslator.FromHtml
method to convert a hexadecimal color code to a Color
in C#. Here is an example:
string hexColor = "#FF0000"; Color color = ColorTranslator.FromHtml(hexColor);
This will convert the hexadecimal color code "#FF0000"
to a Color
with the value Color.Red
.
You can use the ColorTranslator.ToHtml
method to get a hexadecimal color code from a Color
in C#. Here is an example:
Color color = Color.Red; string hexColor = ColorTranslator.ToHtml(color);
This will get the hexadecimal color code "#FF0000"
from the Color
with the value Color.Red
.
You can use the ColorTranslator.FromHtml
method with a modified CSS color code to convert an RGB or RGBA color format to a hexadecimal color format in C#. Here is an example:
string cssColor = "rgb(255, 0, 0)"; if (cssColor.StartsWith("rgb")) { cssColor = "#" + string.Join("", cssColor.Split('(', ')')[1].Split(',').Select(x => int.Parse(x).ToString("X2"))); }
This will convert the RGB color code "rgb(255, 0, 0)"
to the hexadecimal color code "#FF0000"
. If the CSS color code is in RGBA format, the code will be converted to an RGB format by removing the alpha component.
Color Class in C#:
The Color
class in C# is part of the System.Drawing
namespace and represents a color in the RGB color space.
Color myColor = Color.Red;
System.Drawing.Color in C#:
Use System.Drawing.Color
to work with colors in C#.
System.Drawing.Color myColor = System.Drawing.Color.Blue;
RGB Color in C#:
Create a color using RGB components.
Color rgbColor = Color.FromArgb(255, 0, 0); // Red
Creating Colors in C#:
Create colors using different constructors and formats.
Color color1 = Color.Red; Color color2 = Color.FromArgb(255, 0, 0); Color color3 = ColorTranslator.FromHtml("#FF0000");
Setting Color in WinForms C#:
Set the color of UI elements in a Windows Forms application.
button1.BackColor = Color.Green;
Color Conversion in C#:
Convert colors between different representations, such as RGB and HTML.
Color rgbColor = Color.FromArgb(255, 0, 0); string htmlColor = ColorTranslator.ToHtml(rgbColor);
Changing Background Color in C#:
Change the background color of a control or form.
this.BackColor = Color.LightBlue;
Color Dialog in C#:
Use a color dialog to allow users to pick a color.
ColorDialog colorDialog = new ColorDialog(); if (colorDialog.ShowDialog() == DialogResult.OK) { Color selectedColor = colorDialog.Color; }
Color Constants in C#:
Use predefined color constants.
Color blackColor = Color.Black;
Drawing with Colors in C#:
Use colors when drawing on a canvas or control.
Graphics graphics = // Obtain Graphics object Pen pen = new Pen(Color.Blue); graphics.DrawLine(pen, 0, 0, 100, 100);
Color Blending in C#:
Blend colors to create smooth transitions.
Color blendedColor = Color.FromArgb( (color1.R + color2.R) / 2, (color1.G + color2.G) / 2, (color1.B + color2.B) / 2 );
Color Palettes in C#:
Define and use color palettes for consistency in your application.
List<Color> myPalette = new List<Color> { Color.Red, Color.Green, Color.Blue };