rgb values to 0 to 1 scale

112,328

Solution 1

It's simply a case of dividing your RGB value, call it x by 255:

If x = 95 then your value is 95/255 = 0.373 (to 3 d.p.)

Solution 2

a = x / 255

or

x = a * 255

where x is the RGB value and a is your desired result.

Solution 3

if you want to continue using 0-255 can create a function for this as the following example.

function setFillColor2(...)
   local object,r,b,g=...
   object:setFillColor( r/255, b/255,g/255)
end

circle1 = display.newCircle(150,250, 50 ) 
setFillColor2(circle1,23,255,12)

Solution 4

The simplest way if to divide by either 255 for 0-1 or 256 for 0 - a bit less than 1. The latter sometimes has advantages. You need to do the divisions in fixed or floating point, not in integer arithmetic, of course.

However in fact the human response to an rgb channel value on 0-255 is not linear, neither is the device's response. Often you need to do gamma correction. It all gets very involved very quickly, and it doesn't usually matter much for low end graphics. For high end graphics, however, you often want to be out of rgb colourspace altogether, and then you need non-linear conversion functions to finally flush the rgb pixels to the end image.

Share:
112,328

Related videos on Youtube

el.severo
Author by

el.severo

Updated on April 12, 2022

Comments

  • el.severo
    el.severo about 2 years

    I'm trying to calculate some RGB colors (0 - 255) for a 0 to 1 scale. Does anyone knows a online converter or it exists a math formula?

    Lets say that I want to convert 125 RGB with (0 to 255 scale) to a 0 to 1 scale.

    • MikkoP
      MikkoP about 12 years
      Maybe divide by 255?
    • Andy K
      Andy K about 8 years
      Excellent @el.severo