我一直在寻找在开放式办公室xml docx文档中对颜色应用tint
和shade
的算法。我终于找到了一个可能的答案at this link。但现在我无法理解代码,因为我不知道它是什么语言,所有0#
和1#
值对我没有任何意义。
例如,在链接中的以下函数中,0#
和1#
代表什么?
Public Function sRGB_to_scRGB(value As Double)
If value < 0# Then
sRGB_to_scRGB = 0#
Exit Function
End If
If value <= 0.04045 Then
sRGB_to_scRGB = value / 12.92
Exit Function
End If
If value <= 1# Then
sRGB_to_scRGB = ((value + 0.055) / 1.055) ^ 2.4
Exit Function
End If
sRGB_to_scRGB = 1#
End Function
或者任何人都可以告诉我它是什么语言?
Thanx提前!!
答案 0 :(得分:0)
这是Visual Basic(在链接中很难过) 1#表示整数see this
为1我在我的Ruby程序中使用该代码 - 也许这更容易阅读
def sRGB_to_scRGB(color)
if color < 0
0
elsif color <= 0.0031308
color * 12.92
elsif color < 1
1.055 * (color ** (1.0/2.4)) - 0.055
else
1
end
end
答案 1 :(得分:0)
我也阅读了你上面链接的帖子,对我来说非常好。如果你仍然不明白,我在这里给你留下伪代码的翻译:
//Given a string with the hexadecimal RGB color,
//separate it in its three components in integer
Integer red = fromHexaToInt(RGB_string.Substring(0,2));
Integer green = fromHexaToInt(RGB_string.Substring(2,4));
Integer blue = fromHexaToInt(RGB_string.Substring(4,6));
//Convert each integer to a proportion of the maximum value (255)
Double dRed = red / 255;
Double dGreen = green / 255;
Double dBlue = blue / 255;
//Apply the conversion function for each one
dRed = sRGB_to_scRGB(dRed);
dGreen = sRGB_to_scRGB(dGreen);
dBlue = sRGB_to_scRGB(dBlue);
//Apply to each one the shade (must be a number between 0 and 1), let's say 0.4
//NOTE: if you want to use tint, just call that function instead of shade
Double level = 0.4;
dRed = shade(dRed, level);
dGreen = shade(dGreen, level);
dBlue = shade(dBlue, level);
//Apply the inverse of the conversion function
dRed = scRGB_to_sRGB(dRed);
dGreen = scRGB_to_sRGB(dGreen);
dBlue = scRGB_to_sRGB(dBlue);
//Recover the integer value from the converted new color by multiplying and rounding it
red = round(dRed * 255)
green = round(dGreen * 255)
blue = round(dBlue * 255)
//Transform the integers back to hexadecimal
String strRed = fromIntToHexa(red);
String strGreen = fromIntToHexa(green);
String strBlue = fromIntToHexa(blue);
//Concatenate them all
String newColor = strRed + strGreen + strBlue;
你调用的函数可能是这样的:
//Conversion function
function sRGB_to_scRGB(value){
if (value < 0){
return 0;
}
if(value <= 0.04045){
return value / 12.92;
}
if (value <= 1){
return ((value + 0.055) / 1.055) ^ 2.4);
}
return 1;
}
//Function to shade the color
function shade(color, shade){
if(color * shade < 0){
return 0;
}
else{
if(color * shade > 1){
return 1;
}
else{
return color * shade;
}
}
}
//Function to tint color
function tint(color, tint){
if(tint > 0){
return color * (1-tint)+tint;
}
else{
return color * (1 + tint);
}
}
//Reconversion function
function scRGB_to_sRGB(value){
if(value < 0){
return 0;
}
if (value <= 0.0031308){
return value * 12.92;
}
if(value < 1){
return 1.055 * (value ^ (1 / 2.4)) - 0.055;
}
return 1;
}
希望它会有用。