Reputation: 2277
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.
Upvotes: 56
Views: 151954
Reputation: 5264
use like this
color: rgb(140 / 255, 180 / 255, 205 / 255),
divide 255 to each actual value , its always falls between 0-1 scale Actual RGB Color : 140 180 205 0-1 scale code - rgb(140 / 255, 180 / 255, 205 / 255)
Upvotes: 3
Reputation: 6406
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.
Upvotes: 2
Reputation: 11
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)
Upvotes: 1
Reputation: 13275
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.)
Upvotes: 103
Reputation: 74036
a = x / 255
or
x = a * 255
where x
is the RGB value and a
is your desired result.
Upvotes: 14