Home > Software engineering >  How to Convert Hexcolor to RGB color using flutter dart
How to Convert Hexcolor to RGB color using flutter dart

Time:01-31

I want to convert Hex color values to RGB color values. How do I do that? Sorry this question might be a bit short. but the meaning is exactly Because I haven't seen these answers anywhere.
For example, HEX color value = "0xff4f6872" converts to RGB color value = (R:79 G:104 B:114).

I look forward to these answers, thank you.

CodePudding user response:

0xff4f6872 isn‘t (R:79 G:104 B:114).

A hexadecimal color is specified with: #RRGGBB. To add transparency, add two additional digits between 00 and FF.

Here:

ff = 256 (R)
4f = 79 (G)
68 = 104 (B)
72 = 114 this means alpha = 0.45 (114/256)

CodePudding user response:

The built-in material Color class has properties that hold the color value of each color channel. You can use them to find the RGB (red, green blue) or even the RGBA (red, green, blue, alpha) value of a color.

You first need to create a color object for your hex color by putting the value inside the Color() method and add '0xff' before the hex value.

Color myColor = Color(0xffe64a6f);

You can then access any of the properties you want and use/display them however you want

Column(
      children: [
          Text('red value: ${myColor.red}'),
          Text('green value: ${myColor.green}'),
          Text('blue value: ${myColor.blue}'),
          Text('alpha value: ${myColor.alpha}'),
      ],
)
  • Related