The correct method to convert the hexadecimal color value to UnityColor solves the problem of displaying pure white

preface

The principle of converting HEX16 base color value to RGB is very simple: take the first two digits and convert them to 10 base, that is, R; Take the middle two digits and convert them to decimal system, that is, G; Take the last two digits and convert them to decimal, that is, B.

But if you use this RGB value to create a new Color, the resulting color is always white or black.

reason

The value of the three (or four, if alpha) parameters in the Unity Color construction method is not 0-255, but 0-1, which represents the color percentage. We need to divide the RGB value by 255 to get the desired scale value.

code

 //16 bit color to UnityColor color public Color GetColor(string color) { if (color. Length == 0) { Return Color. black;//Set to black } else { //#Ff8c3 removal# color = color.Substring(1); int v = int.Parse(color,  System.Globalization.NumberStyles.HexNumber); //Convert Color return new Color( //Int>>Shift to low order //&Bit by bit and de high bit ((float)(((v >> 16) & 255))) / 255, ((float)((v >> 8) & 255)) / 255, ((float)((v >> 0) & 255)) / 255 ); } }

Postscript

Other people's blogs pursue the quality of articles, but my articles are too poor and can only rely on quantity.

References

1. [Header] [Unity] Unity Japan UnityChanSD Role

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/4146.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. Panda A 09-01 21:54 reply

    Alas, the term "16 bit color" is a bit inaccurate. It should be "hexadecimal". At first sight, I didn't understand why there are 16 colors, and I thought for a long time why only the first two middle and last two haha.

    • hare 09-02 00:03 reply

      Oh, oh, we are used to working :mrgreen:

      • Gazzz 09-02 07:28 reply

        Are hares now engaged in front-end or design work?

        • hare 09-02 10:43

          Client development.

    • hare 09-02 00:05 reply

      Corrected.

  2. littleplus 09-03 01:15 reply

    Stupid bunny Stupid bunny Stupid bunny, repeat important things three times Hee hee

  3. mikusa 09-05 14:58 reply

    Stupid rabbit Stupid rabbit Stupid rabbit Stupid rabbit Stupid rabbit, repeat important things six times Hee hee

  4. Panda A 09-19 17:32 reply

    Wow, the advertisement is so crazy. There are articles in front of the article, windows at the top, documents at the end and