Launch a Color Value Conversion Tool

Launch a Color Value Conversion Tool

Convert between HEX, RGB, RGBA, ARGB, and HSL

Last updated 7/4/2023 10:49 PM
沙漠尽头的狼
4 min read
Category
Blazor
Tags
.NET Blazor Color Conversion

Hello everyone, I am Wolf at the End of the Desert.

A few days ago, I just launched a color value conversion tool, which is of course a reference from others. It can:

  1. Convert between HEX, RGB, RGBA, ARGB, and HSL;
  2. Display a very practical CSS color table for use during development: 147 color names defined in the HTML and CSS color specification (17 standard colors plus 130 additional colors).

This article briefly lists a few places where the JS code and C# code were compared during the development of this tool, for your reference in similar future development.

JS file: Click here

1. HEX Color Value Conversion

Extract the R (Red), G (Green), B (Blue) values from the HEX #9A3B34.

JS code

function parseHEX(val) {
    let color = new Color();
    try {
        let rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
        let hex = val.replace(rgx, function (m, r, g, b) {
            return r + r + g + g + b + b;
        });
        let rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
        color.r = parseInt(rgb[1], 16);
        color.g = parseInt(rgb[2], 16);
        color.b = parseInt(rgb[3], 16);
    } catch (e) {
        console.log(e)
    }
    return color;
}

C# code

public static Color? ParseHEX(string hexColor)
{
    hexColor = hexColor.TrimStart('#');

    Regex regex = new(@"^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$");
    var match = regex.Match(hexColor);

    if (match?.Success == true)
    {
        int red = Convert.ToInt32(match.Groups[1].Value, 16);
        int green = Convert.ToInt32(match.Groups[2].Value, 16);
        int blue = Convert.ToInt32(match.Groups[3].Value, 16);

        return new Color(red, green, blue);
    }
    else
    {
        return null;
    }
}

2. RGBA Color Value Conversion

The conversion logic is basically the same as above.

JS code

function parseRGBA(val) {
    let color = new Color();
    try {
        let rgba = /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/.exec(val);
        color.r = parseInt(rgba[1]);
        color.g = parseInt(rgba[2]);
        color.b = parseInt(rgba[3]);
        color.a = parseFloat(rgba[4] || 1);
    } catch (e) {
        console.log(e)
    }
    return color;
}

C# code

public static Color? ParseRGBA(string color)
{
    Regex regex = new Regex(@"^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$");
    var match = regex.Match(color);

    if (match?.Success == true)
    {
        int r = int.Parse(match.Groups[1].Value);
        int g = int.Parse(match.Groups[2].Value);
        int b = int.Parse(match.Groups[3].Value);
        if (!double.TryParse(match.Groups[4].Value, out double a))
        {
            a = 1;
        }

        return new Color(r, g, b, a);
    }

    return null;
}

Using ChatGPT, it is very convenient to translate JS code into C# code, making it quick to build a Blazor tool. Other online programming language translation websites are not ideal, and most of the time manual adjustments are still needed.

Keep Exploring

Related Reading

More Articles
Same category / Same tag 11/6/2024

Why My Blog Website Returned to Blazor

The development of the blog website has gone through many hardships, with nearly 10 versions including MVC, Vue, Go, etc. Now it has returned to Blazor and adopted static SSR, resulting in a significant speed increase and successful launch.

Continue Reading
Same category / Same tag 2/29/2024

Data Display Can Also Be Done Like This in Winform

In the process of developing Winform, data display functionality is often required. Previously, the gridcontrol control was commonly used. Today, through an example, I would like to introduce how to use the table component from Ant Design Blazor for data display in a Winform Blazor Hybrid application.

Continue Reading
Same category / Same tag 2/29/2024

Can the Winform interface also look good?

A few days ago, I introduced using Blazor Hybrid in Winform, and mentioned that with the Blazor UI, our Winform programs can be designed to look better. Next, I will illustrate with an example of drawing in Winform Blazor Hybrid, hoping it helps you.

Continue Reading
Same category / Same tag 1/7/2024

Code Workshop 'Article Title URL Alias Generator' Launched

Code Workshop is a new open-source project by the webmaster that provides web online tools, cross-platform desktop and mobile applications. The webmaster is committed to bringing you a more efficient and convenient experience. Today, the webmaster is proud to launch the 'Article Title URL Alias Generator', helping you easily create URL aliases for article titles, improving SEO and user experience. Come to Code Workshop and explore more practical tools!

Continue Reading