How do I convert RGB to float values?

17,566

Depending on the range of the int RGB values, you can simply divide by the maximum brightness value allowed to convert to float.

For example, if the int ranges from 0 - 255 (e.g. HTML Color Codes), then you can use

float fR = intR / 255.0F;

and so on for G and B.

Share:
17,566
GibEinenNamenEin
Author by

GibEinenNamenEin

Updated on June 27, 2022

Comments

  • GibEinenNamenEin
    GibEinenNamenEin almost 2 years

    im getting 3 ints: R, G, B I want to use this colors for something where i need to enter 3 floats: R, G, B. How do I "convert" the int to a float? Because in the float 1 is the highest

    Thanks

  • GibEinenNamenEin
    GibEinenNamenEin over 5 years
    what is iR? Is it the int?
  • NetMage
    NetMage over 5 years
    @RarePotato8DE Yes that is the int R value.
  • Didi Kohen
    Didi Kohen over 3 years
    Why is it 255 and not 256? Aren't there 256 possible values for each color?
  • NetMage
    NetMage over 3 years
    @DidiKohen Yes, but the range of values is 0 to 255. You want to match those values to the range 0.0 - 1.0, you want 0 to return 0 and you want 255 to return 1.0.