Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Convert Hexadecimal to RGB

Syntax: hexToRgb("#454545") or hexToRgb([45, 45, 45])

function hexToRgb(a) {
       if(typeof a == "string")
               a = a.match(/\w\w/g);
       return [ "0x" + a[0] - 0, "0x" + a[1] - 0, "0x" + a[2] - 0 ];
}



Convert Hue/Sat/Lum to Hexadecimal hue: 0-239, sat: 0-240, lum: 0-240

Syntax: hslToHex(40, 143, 202)

function hslToHex(a, b, c) {
       var d = 3, e = a / 40 >> 0, f = 0.025 * (a % 40), g = [ e % 5 < 2, e && e < 4, e > 2 ];
               g[2 - (1 + e) % 3] -= (e & 1) * 2 * f - f;
       while(d --) {
               g[d] *= 255;
               g[d] -= (g[d] - 127) * (1 - b / 240);
               g[d] = (g[d] - (c > 120 ? 255 - g[d] : g[d]) * (1 - c / 120) + 0.5 >> 0).toString(16);
       }
       return g;
}



Convert Hue/Sat/Lum to RGB hue: 0-239, sat: 0-240, lum: 0-240

Syntax: hslToRgb(40, 143, 202)

function hslToRgb(a, b, c) {
       var d = 3, e = a / 40 >> 0, f = 0.025 * (a % 40), g = [ e % 5 < 2, e && e < 4, e > 2 ];
               g[2 - (1 + e) % 3] -= (e & 1) * 2 * f - f;
       while(d --) {
               g[d] *= 255;
               g[d] -= (g[d] - 127) * (1 - b / 240);
               g[d] = g[d] - (c > 120 ? 255 - g[d] : g[d]) * (1 - c / 120) + 0.5 >> 0;
       }
       return g;
}



Convert RGB to Hexadecimal

Syntax: rgbToHex([255, 0, 255]) or rgbToHex("rgb(255,0,255)")

function rgbToHex(a) {
       if(typeof a == "string") {
               a = a.match(/\d+/g);
               a = [ a.split(",")[0] - 0, a.split(",")[1] - 0, a.split(",")[2] - 0 ];
       }
       return [
               (a[0] < 16 ? "0" : "") + a[0].toString(16),
               (a[1] < 16 ? "0" : "") + a[1].toString(16),
               (a[2] < 16 ? "0" : "") + a[2].toString(16)
       ];
}



Notes: If you'd like to play around with the hue order in the HSL conversions, replace the first two lines (those preceding the for loop) with this:

var d=3,g=[[[255,0,0],1,1],[[255,255,0],0,-1], [[0,255,0],2,1],[[0,255,255],1,-1],[[0,0,255],0,1], [[255,0,255],2,-1]][Math.floor(a/40)];
g[0][g[1]]+=Math.floor(6.375*(a%40))*g[2];g=g[0];


And move the [[R,G,B],#,#] elements around as you please.

Edit: Forgot to change a function name.


Last Edit: Aug 17, 2010 20:43:20 GMT by Aaron

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
hslToHex and hslToRgb... notice how they have internal repetition? I'm wondering if it's worth using a loop or making a simple function inside to compress it to avoid repeating nearly identical lines?

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
If it were half a dozen lines, i'd agree. But at three, I figured why opt for aesthetics over performance? As you've mentioned it, though:

Rewrote the hue selection bit and used a for loop for the sat/lum equations.

function hslToRgb(a, b, c) {
var d=Math.floor(a/40),e=d+1,f=6.375*(a%40), g=[(d>4||d<2)*1,(d&&d<4)*1,(d>2)*1];
g=[g[0]*255+((e>4||e<2)*1-g[0])*f, g[1]*255+((e<4)*1-g[1])*f, g[2]*255+((e>2&&e<6)*1-g[2])*f];
for(var h=0;h<3;h++)
g[h]-=(g[h]-127)*(240-b)/240, g[h]=Math.round(g[h]-(c>120?255-g[h]:g[h])*((120-c)/120));
return g;
}


Edit: Spaced to remove stretching.


Last Edit: Aug 2, 2010 10:53:22 GMT by Aaron

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
The performance change is so insignificant that it could be ignored in most coding. :P I just figured I'd throw it out there either way though.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
And for three lines, no more worth the effort, aye? =P

Eric

Eric Avatar



1,442


November 2005
Aaron Avatar
And for three lines, no more worth the effort, aye? =P
In this code, it might not matter because this is kind of a "set" equation, but for other code, should that line change you then have 2 other places you have to change it. Or, if you have both functions, you then have 5 other places you have to change it. In this case though, yeah, not a big deal, otherwise, yeah.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Aaron: Had to edit your first post to prevent stretching in v1.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Alright, compressed and updated.

Chris: The whole of the forum stretches in Opera, so it's hard to tell. :x

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Really? :P You never reported this? what skin?

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
v1: Ex (when i'm logged in).

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I'll look into it later. I have an idea what it might be.

Jim

Jim Avatar
Josh was here.

****
Studio Member

740


September 2005
Chris Avatar
I'll look into it later. I have an idea what it might be.


The quick reply stretches in Opera on version 2.1, by the way :P. I just so rarely go down that far on the page because I never use it... so I've never noticed until yesterday.


Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Yeah, the quick reply was my guess. Honestly, it's because rows/cols is not standardized between browsers. Why not? I have no fucking idea, but it pisses me off. :P It's the same reason for the issue on Linux and Safari. I'll try to fix it tonight.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Compressed again. Wonder why JS doesn't support floating points in binary operations?

a & a, a >> 0, etc. seem to be fast alternatives to Math.floor.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
What were the originals again? For comparisons sake. :P

newBookmarkLockedFalling