# 在白色上将RGB转换为RGBA

2020/10/18 09:41 · javascript ·  · 0评论

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

``````function RGBtoRGBA(r, g, b){
if((g == null) && (typeof r === 'string')){
var hex = r.replace(/^\s*#|\s*\$/g, '');
if(hex.length === 3){
hex = hex.replace(/(.)/g, '\$1\$1');
}
r = parseInt(hex.substr(0, 2), 16);
g = parseInt(hex.substr(2, 2), 16);
b = parseInt(hex.substr(4, 2), 16);
}

var min, a = (255 - (min = Math.min(r, g, b))) / 255;

return {
r    : r = 0|(r - min) / a,
g    : g = 0|(g - min) / a,
b    : b = 0|(b - min) / a,
a    : a = (0|1000*a)/1000,
rgba : 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')'
};
}

RGBtoRGBA(204, 153, 102) == RGBtoRGBA('#CC9966') == RGBtoRGBA('C96') ==
{
r    : 170,
g    : 85 ,
b    : 0  ,
a    : 0.6,
rgba : 'rgba(170, 85, 0, 0.6)'
}
``````

``````152 converts to an 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
``````

``````r = a' * r' + 1 - a'
g = a' * g' + 1 - a'
b = a' * b' + 1 - a'
``````

1-a'项代表背景，其他项代表前景。做一些代数：

``````r = a' * (r' - 1) + 1
r - 1 = a' * (r' - 1)
(r - 1) / (r' - 1) = a'
(r' - 1) / (r - 1) = 1 / a'
r' - 1 = (r - 1) / a'
r' = (r - 1) / a' + 1
``````

``````m = min(r, g, b)
``````

``````0 = (m - 1) / a' + 1
-1 = (m - 1) / a'
-a' = m - 1
a' = 1 - m
``````

``````function rgba(r, g, b) {
var a = 1 - Math.min(r, Math.min(g, b)) / 255;
return [255 + (r - 255) / a, 255 + (g - 255) / a, 255 + (b - 255) / a, a];
}
``````

``````L = (MAX(R,G,B) + MIN(R,G,B))/2
L1 = (255 + 128) / 2 = 191.5
L2 = (128 + 0) /2 = 64
A = (191,5 - 64) / 255 = 0,5;
``````

``````@function transparentize-on-white(\$color)
{
\$red: red(\$color);
\$green: green(\$color);
\$blue: blue(\$color);
\$lowestColorComponent: min(\$red, \$green, \$blue);
\$alpha: (255 - \$lowestColorComponent) / 255;

@return rgba(
(\$red - \$lowestColorComponent) / \$alpha,
(\$green - \$lowestColorComponent) / \$alpha,
(\$blue - \$lowestColorComponent) / \$alpha,
\$alpha
);
}
``````

``````x = a + (1 - a) x'
y = a + (1 - a) y'
z = a + (1 - a) z'
``````

``````x = 255 a + (1 - a) x'
y = 255 a + (1 - a) y'
z = 255 a + (1 - a) z'
``````

``````a  = (x - x')/(255 - x')          x' = (x - 255 a)/(1 - a)
``````

``````let x = min(r,g,b)
a = 1 - x/255                    # Correction 1
r,g,b = ( (r,g,b) - x ) / a      # Correction 2
``````

``````//Convert RGB to HSL
hsl = new HSL(rgb)

//Use lightness as alpha
alpha = hsl.Lightness

//For #80000 lightness is 0.5, so we have to take this into account.
//Lightness more than 0.5 converts to alpha 1 and below is a linear ratio
if (alpha > 0.5)
{
alpha = 1;
}
else
{
alpha = alpha / 0.5;
//We need to drop the lightness of the color to 0.5 to get the actual color
//that needs to display. Alpha blending will take care of that.
hsl.Lightness = 0.5;
}

newRgb = hsl.convertToRgb()
``````

“ newRgb”将包含新调整的颜色的值，并使用“ alpha”变量来控制透明度。