javascript - Convert RGB to RGBA over white -



javascript - Convert RGB to RGBA over white -

i have hex color, e.g. #f4f8fb (or rgb(244, 248, 251)) want converted as-transparent-as-possible rgba color (when displayed on white). create sense? i'm looking algorithm, or @ to the lowest degree thought of algorithm how so.

for example:

rgb( 128, 128, 255 ) --> rgba( 0, 0, 255, .5 ) rgb( 152, 177, 202 ) --> rgba( 50, 100, 150, .5 ) // can better(lower alpha)

ideas?

fyi solution based on guffa's answer:

class="lang-js prettyprint-override">function rgbtorgba(r, g, b){ if((g==void 0) && (typeof r == 'string')){ r = r.replace(/^\s*#|\s*$/g, ''); if(r.length == 3){ r = r.replace(/(.)/g, '$1$1'); } g = parseint(r.substr(2, 2), 16); b = parseint(r.substr(4, 2), 16); r = parseint(r.substr(0, 2), 16); } var min, = ( 255 - (min = math.min(r, g, b)) ) / 255; homecoming { r : r = 0|( r - min ) / a, g : g = 0|( g - min ) / a, b : b = 0|( b - min ) / a, : = (0|1000*a)/1000, rgba : 'rgba(' + r + ', ' + g + ', ' + b + ', ' + + ')' }; } rgbtorgba(204, 153, 102) == rgbtorgba('#cc9966') == rgbtorgba('c96') == { r : 170, g : 85 , b : 0 , : 0.6, rgba : 'rgba(170, 85, 0, 0.6)' }

take lowest color component, , convert alpha value. scale color components subtracting lowest, , dividing alpha value.

example:

152 converts alpha value of (255 - 152) / 255 ~ 0.404 152 scales using (152 - 152) / 0.404 = 0 177 scales using (177 - 152) / 0.404 ~ 62 202 scales using (202 - 152) / 0.404 ~ 123

so, rgb(152, 177, 202) displays rgba(0, 62, 123, .404).

i have verified in photoshop colors match perfectly.

javascript css colors css3 rgba

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -