Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting the Alpha in RGBA to CSS Opacity

Tags:

css

php

image

I am trying to get the transparency from RGBA and convert it to CSS opacity.

So after getting the RGBA of an image pixel using the following:

$rgb = imagecolorat($img, $j, $i);
$colors = imagecolorsforindex($img, $rgb);
// Printing colors, I get
// 255,255,255,127
// 249,161,66,126
//...

For some reason, the transparency is always an integer (i.e. 127) and not a decimal number. But I want to convert that 127 figure to something I can use in CSS:

opacity:0;filter:alpha(opacity=0)

However, I can't work out the link between alpha in RGBA and how I can turn it into CSS opacity.

I will be doing this for all sorts of pixels from different types of images. PNG, JPG, GIF

Any ideas?

like image 731
Abs Avatar asked Dec 03 '25 15:12

Abs


1 Answers

opacity = (127 - transparency) / 127.0

like image 121
slash197 Avatar answered Dec 06 '25 04:12

slash197