Mohanapriya R Mohanapriya R
Updated date Jun 17, 2024
This article will discuss color conversion in C# applications. Learn how to convert strings into vibrant colors with a simple example and output.

String to Color Conversion in C#:

C# provides a simple way to convert strings to colors through the Color class in the System.Drawing namespace. The ColorTranslator class, specifically the FromHtml method, used to convert hexadecimal color representations in strings into Color objects.

using System;
using System.Drawing;

class Program
    static void Main()
        Console.Write("Enter a color code (e.g., #RRGGBB): ");
        string colorCode = Console.ReadLine();

        // Convert string to color
        Color color = ColorTranslator.FromHtml(colorCode);

        // Display the result
        Console.WriteLine($"The converted color is: {color}");

The above program prompts the user to input a color code in the form of "#RRGGBB," where RR, GG, and BB are hexadecimal values representing the red, green, and blue components of the color, respectively. The ColorTranslator.FromHtml method is used to convert the input string to a Color object.


Enter a color code (e.g., #RRGGBB): #28465a
The converted color is: Color [A=255, R=40, G=70, B=90]

Comments (0)

There are no comments. Be the first to comment!!!