Question

I have a rgba color in which alpha value is in 0-255 range. now i am using this function imagecolorallocatealpha in php it accept alpha value in 0-127 range.how can i convert my alpha value that is in 0-255 to 0-127

for example 
Alpha = 255
equivalent in 0-127 = 127

Alpha = 0
equivelent in 0-127 = 0
Was it helpful?

Solution

use this code

imagecolorallocatealpha($image, $Red, $Green, $Blue, $Alpha/2);

instead of

imagecolorallocatealpha($image, $Red, $Green, $Blue, $Alpha);

Example 
Alpha = 255
equivalent in 0-127 = 127  as (255/2 = 127.5)

Alpha = 0
equivelent in 0-127 = 0   as (0/2 = 0)

OTHER TIPS

As the comment suggest, dividing by 2 seems like a good idea.

$Alpha_in_127_scale = round($Alpha / 2, 0);

I know round() rounds to zero decimal places by default, but it's different in different languages, so I like to specify it for easier reading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top