1

I have been looking for algorithms to apply tint and shade on colours in open office xml docx documents. I finally found a possible answer at this link. But now i have trouble understanding the code because i don't know what language it is and all the 0# and 1# values are not making any sense to me. For example in the following function in the link, what does 0# and 1# stands for?

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

Or can anyone tell me what language it is?

Thanx in advance!!

Surender Thakran
  • 3,958
  • 11
  • 47
  • 81

2 Answers2

1

I also read the post you linked above and worked very well for me. If you still don't understand it, here I leave you the translation to pseudo-code:

//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;

And the functions you call may be like this:

//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;       
}

Hope it will be useful.

AngelBlond8
  • 433
  • 1
  • 3
  • 12
0

This is Visual Basic (as sad in your link) 1# means it is 1 in Integer see this

I use that code in my Ruby program - maybe this is more easy to read

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
ShockwaveNN
  • 2,227
  • 2
  • 29
  • 56