Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert RGBA css color format to hexadecimal format

In my selenium code i need to verify that color code is #192856 for background. but when i get the CSS property of that element it is giving me color in rgba format. Now i need to get values in hex values itself . How can i do that?

quickLinkstab.GetCssValue("background-color")

above is givingm e value of "rgba(25, 40, 86, 1)" which is rgba value. Is there any way i can convert it back to Hex ? or i can get value in Hex itself?

i've also tried below code

 string colorcode = menuColor;
        int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
        Color clr = Color.FromArgb(argb);


        int r = Convert.ToInt16(clr.R);
        int g = Convert.ToInt16(clr.G);
        int b = Convert.ToInt16(clr.B);
        int a = Convert.ToInt16(clr.A);
        string x = string.Format("rgba({0}, {1}, {2}, {3});", r, g, b,a);

but this one is giving me value like , "rgba(25, 40, 86, 0);" . Difference in "a" value. Like my code gives me 0 for "a" but cssvalue is 1.

I'm more looking towards the solution of getting Hex value directly or if not possible then convert rgba to Hex.

like image 206
vic Avatar asked Jul 21 '16 00:07

vic


People also ask

How do you convert RGBA to hexadecimal?

Converting RGBA to hex with the #rgba or #rrggbbaa notation follows virtually the same process as the opaque counterpart. Since the alpha ( a ) is normally a value between 0 and 1, we need to multiply it by 255, round the result, then convert it to hexadecimal.

Is RGBA the same as hex?

RGBA (Red, Green, Blue, Alpha) is used for making a colour transparent. The value for A (alpha) is from 0, completely transparent, to 1 completely opaque. hex, is a more recent quick easy value used exclusively for websites and applications.

Can I use hex in RGBA?

In CSS, there are several formats for colors that can be used. Common ones include hex (hexadecimal) codes, RGB (red, green, blue), RGBA (red, green, blue, alpha), and HSL (hue, saturation, lightness).

What does RGBA 255 0 0 0.2 color code in CSS means?

Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255. For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0. To display black, set all color parameters to 0, like this: rgb(0, 0, 0).


1 Answers

using System.Drawing.ColorTranslator

string htmlColor = ColorTranslator.ToHtml(myColor);

using String.Format

//RGB
String.Format("#{0:X2}{1:X2}{2:X2}", colorValue.R,colorValue.G,colorValue.B);
//RGBA
String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", colorValue.A,colorValue.R,colorValue.G,colorValue.B);

Extenstion

public static class ColorExtension
{
    public static string HexFormat(this Color colorValue)
    {
        return String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", colorValue.A, colorValue.R, colorValue.G, colorValue.B);
    }
}
like image 89
oudi Avatar answered Sep 28 '22 17:09

oudi