I finally got code to format correctly in my Word Press configuration, so I figure I should start putting some code down. I’m going to begin by sharing my RGB and HSL conversion formulas that I converted from easyrgb.com formulas into JavaScipt functions for my color tool.
Before I begin, I have to share these required functions that are necessary in order for the conversion to work:
[js]
function min_three_args(argOne,argTwo,argThree){
var minValue;
minValue = Math.min(argOne,argTwo);
minValue = Math.min(minValue,argThree);
return minValue;
}
function max_three_args(argOne,argTwo,argThree){
var maxValue;
maxValue = Math.max(argOne,argTwo);
maxValue = Math.max(maxValue,argThree);
return maxValue;
}
[/js]
They just figure out which of three arguments has the maximum value, something not native to JavaScript.
Here are the conversion functions. These two functions convert HSL into RGB. HSL values will range from 0 to 1. RGB values will vary from 1 to 255. The return will be an associative array. If you did returnR = hsl_to_rgb(R,G,B); then returnR[’b'] would have the blue value.
[js]
function hsl_to_rgb(H,S,L){
//formula from easyrgb.com converted to JavaScript by me
var R;
var G;
var B;
var var_1;
var var_2;
var returnObject;
returnObject = new Object;
if( S == 0 ){
//HSL values = 0 ? 1
R = L * 255; //RGB results = 0 ? 255
G = L * 255;
B = L * 255;
}else{
if( L < (1/2) ){
var_2 = (L * ( 1 + S ));
}else{
var_2 = (( L + S ) - ( S * L ));
}
var_1 = ((2 * L) - var_2);
R = 255 * Hue_2_RGB( var_1, var_2, (H + ( 1 / 3 )) );
G = 255 * Hue_2_RGB( var_1, var_2, H );
B = 255 * Hue_2_RGB( var_1, var_2, (H - ( 1 / 3 )) );
}
returnObject['r'] = R;
returnObject['g'] = G;
returnObject['b'] = B;
return returnObject;
}
function Hue_2_RGB( v1, v2, vH )
{
//formula from easyrgb.com converted to JavaScript by me
//Function Hue_2_RGB
if( vH < 0 ){
vH += 1;
}
if( vH > 1 ){
vH -= 1;
}
if(( 6 * vH ) < 1 ){
return ( v1 + ( v2 - v1 ) * 6 * vH );
}
if(( 2 * vH ) < 1 ){
return v2;
}
if(( 3 * vH ) < 2 ){
return ( v1 + ( v2 - v1 ) * ( ( 2 / 3 ) - vH ) * 6 );
}
return v1;
}
[/js]
This next function converts RGB to HSL. RBG values will be in decimal and range from 1 to 255. HSL values will vary from 0 to 1. The return will be an associative array. If you did returnR = rgb_to_hsl(R,G,B); then returnR['h'] would have the hue value.
[js]
function rgb_to_hsl(R,G,B){
//formula for conversion from easyrgb.com converted to JavaScript by me
var var_R;
var var_G;
var var_B;
var H;
var S;
var L;
var del_R;
var del_G;
var del_B;
var returnObject;
returnObject = new Object;
var_R = ( R / 255 ); //Where RGB values = 0 ? 255
var_G = ( G / 255 );
var_B = ( B / 255 );
var_Min = min_three_args( var_R, var_G, var_B ); //Min. value of RGB
var_Max = max_three_args( var_R, var_G, var_B ); //Max. value of RGB
del_Max = (var_Max - var_Min) //Delta RGB value
L = (( var_Max + var_Min ) / 2);
if ( del_Max == 0 ) //This is a gray, no chroma...
{
H = 0; //HSL results = 0 ? 1
S = 0;
}else{
//Chromatic data...
if( L < (1/2) ){
(S = del_Max / ( var_Max + var_Min ));
}else{
(S = del_Max / ( 2 - var_Max - var_Min ));
}
del_R = (((( var_Max - var_R ) / 6 ) + ( del_Max / 2 )) / del_Max);
del_G = (((( var_Max - var_G ) / 6 ) + ( del_Max / 2 )) / del_Max);
del_B = (((( var_Max - var_B ) / 6 ) + ( del_Max / 2 )) / del_Max);
if( var_R == var_Max ){
H = del_B - del_G;
}else if ( var_G == var_Max ){
H = (( 1 / 3 ) + del_R - del_B);
}else if ( var_B == var_Max ){
H = (( 2 / 3 ) + del_G - del_R);
}
if( H < 0 ){
H += 1;
}
if( H > 1 ){
H -= 1;
}
}
returnObject[’h'] = H;
returnObject[’s’] = S;
returnObject[’l'] = L;
return returnObject;
}
[/js]
Here is some stuff I wrote to quickly convert a hexidecimal web color into an associative array with corresponding HSL and RGB values. The web color parameter may or may not be prefixed with a hash (#). Note this returns an associative array, and the keys are capitalized. So much for consistency, I know. All the following functions require the previously shown functions.
[js]
function get_color_vals(webColor){
//get R,G,B,H,S,L from webcolor (as in #ffffff, or just ffffff).
//returns associative array/object that includes the original webColor with a #
var returnObj = new Object;
var rawColor;
webColor = String(webColor);
if((webColor.substr(0,1)) == “#”){
returnObj[’webColor’] = webColor;
rawColor = webColor.substr(1);
}else{
returnObj[’webColor’] = “#” + webColor;
rawColor = webColor;
}
//converting #000000 hex rgb to decimal values:
returnObj[’R'] = parseInt(rawColor.substr(0,2),16);
returnObj[’G'] = parseInt(rawColor.substr(2,2),16);
returnObj[’B'] = parseInt(rawColor.substr(4,2),16);
hslObject = rbg_to_hsl(returnObj[’R'],returnObj[’G'],returnObj[’B']);
returnObj[’H'] = hslObject[’h'];
returnObj[’S'] = hslObject[’s’];
returnObj[’L'] = hslObject[’l'];
return returnObj;
}
[/js]
This code will turn HSL values ranging from 0 to 1 into a hexdecimal web color. It does not prefix a hash.
[js]
function hsl_to_webColor(h,s,l){
//converting HSL to RGB webcolor
//does not prefix a ‘#’
var rgbObject;
var rgb;
var r_dec;
var g_dec;
var b_dec;
var r_hex;
var g_hex;
var b_hex;
var concat_pad = “0″;
rgbObject = hsl_to_rgb(h,s,l); //calling function with converted formula from easyrbg.com
r_dec = Math.abs(Math.floor(rgbObject[’r']));
g_dec = Math.abs(Math.floor(rgbObject[’g']));
b_dec = Math.abs(Math.floor(rgbObject[’b']));
if(r_dec > 255){
r_dec = r_dec - 255;
}else if(r_dec < 0){
r_dec = 0;
}
if(g_dec > 255){
g_dec = g_dec - 255;
}else if(g_dec < 0){
g_dec = 0;
}
if(b_dec > 255){
b_dec = b_dec - 255;
}else if(b_dec < 0){
b_dec = 0;
}
r_hex = r_dec.toString(16); //converting to hex
if(r_hex.length < 2){
r_hex = concat_pad.concat(r_hex);
}
g_hex = g_dec.toString(16); //converting to hex
if(g_hex.length < 2){
g_hex = concat_pad.concat(g_hex);
}
b_hex = b_dec.toString(16); //converting to hex
if(b_hex.length < 2){
b_hex = concat_pad.concat(b_hex);
}
rgb = r_hex + g_hex + b_hex;
return rgb;
}
[/js]
This next function is pretty cool, I think. It returns 10 blended colors from two hexidecimal web colors (#ffffff format). You can play with it to increase or decrease the number of blended colors you get. There's a bit of a hack in there, so when you do play with it, take note that I fudged the math to return 10 colors, it's not exact science, so I really mean that you'll have to 'play' with it if you want to change it.
[js]
function get_gradients(webColor1,webColor2){
//I wrote this thing. But...you can have it. ;\
//returns an array of 10 colors that are the intermediary colors
//between webColor1 and webColor2
var webColorObj1;
var webColorObj2;
var temph1;
var temps1;
var templ1;
var temph2;
var temps2;
var templ2;
var difference;
var quotient;
var newH = new Array();
var newS = new Array();
var newL = new Array();
var returnColors = new Array();
webColorObj1 = get_color_vals(webColor1);
webColorObj2 = get_color_vals(webColor2);
//hues
temph1 = Math.floor(webColorObj1['H'] * 255);
temph2 = Math.floor(webColorObj2['H'] * 255);
difference = Math.abs(temph1 - temph2);
if(difference == 0){
for(i=0;i<10;i++){
newH[i] = temph1;
}
}else{
quotient = Math.floor(difference/(10 + (1/2)));
if(temph1 < temph2){
for(i=0;i<10;i++){
newH[i] = Math.floor(temph1 + (quotient * (i + 1)));
}
}else{
for(i=0;i<10;i++){
newH[i] = Math.floor(temph1 - (quotient * (i + 1)));
}
}
}
temps1 = Math.floor(webColorObj1['S'] * 255);
temps2 = Math.floor(webColorObj2['S'] * 255);
difference = Math.abs(temps1 - temps2);
if(difference == 0){
for(i=0;i<10;i++){
newS[i] = temps1;
}
}else{
quotient = Math.floor(difference/(10 + (1/2)));
if(temps1 < temps2){
for(i=0;i<10;i++){
newS[i] = Math.floor(temps1 + (quotient * (i + 1)));
}
}else{
for(i=0;i<10;i++){
newS[i] = Math.floor(temps1 - (quotient * (i + 1)));
}
}
}
//lightness
templ1 = Math.floor(webColorObj1['L'] * 255);
templ2 = Math.floor(webColorObj2['L'] * 255);
difference = Math.abs(templ1 - templ2);
if(difference == 0){
for(i=0;i<10;i++){
newL[i] = templ1;
}
}else{
quotient = Math.floor(difference/(10 + (1/2)));
if(templ1 < templ2){
for(i=0;i<10;i++){
newL[i] = Math.floor(templ1 + (quotient * (i + 1)));
}
}else{
for(i=0;i<10;i++){
newL[i] = Math.floor(templ1 - (quotient * (i + 1)));
}
}
}
//put it all together
for(i=0;i<10;i++){
temph1 = (newH[i]/255);
temps1 = (newS[i]/255);
templ1 = (newL[i]/255);
returnColors[i] = "#" + hsl_to_webColor(temph1,temps1,templ1);
}
return returnColors; //oh yes, I rock. <3
}
[/js]
I don't gurantee anything will work or that I will support it or that if you use this your browser wont explode and if you choose to use this code you agree to do so at your own risk and not sue me, etc.. The conversions were from easyrgb.com, make sure you go check out their informative site.
Feel free to email me (bjorn [at] ambientchill [dot] com) with questions if you have any.
Yes my code is fugly. There are probably some lines in there that I just used for debugging, you might want to remove those, although they should be inactive. It was written by me for me, and I’m just sharing it out of the goodness of my heart so don’t whine.