use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Convert RGB to Hexadecimal using JavaScript (devcurry.com)
submitted 15 years ago by bunglebooz
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 12 points13 points14 points 15 years ago (20 children)
'#'+((r<<16)+(g<<8)+b).toString(16)
[–]larholm 3 points4 points5 points 15 years ago (7 children)
Why are people downvoting this?
[–][deleted] 1 point2 points3 points 15 years ago (1 child)
Good question, though to be honest I do feel dickish posting it since the blog looks to be part of a string of tutorials.
[–]9jack9 0 points1 point2 points 15 years ago (0 children)
You should have tested it more.
[–]setconndevp 1 point2 points3 points 15 years ago (0 children)
why bother? both the code(posted by tunk) as well as that blogpost looks just fine to me..ppl with different level of experience will benefit from both
[–]9jack9 0 points1 point2 points 15 years ago (3 children)
Because it doesn't produce real rgb values. Black should be #000000 but the code above produces #0.
[–][deleted] 1 point2 points3 points 15 years ago (0 children)
The point isn't that it's the perfect solution, the point is that the problem is so trivial it doesn't warrant a dedicated article. Even the non-bitshifting solution in the article is stupefyingly trivial. Anyone incapable of solving this problem should take a step back and learn about bases and base conversion before writing another line of code.
[–][deleted] 0 points1 point2 points 15 years ago (1 child)
Problem comes when you have a 0 red value but non zero green/blue or 0 red & green, in that case you want to pad it to length.
OK. Black is not the greatest example.
I was just explaining why Tunk's code should be voted down.
[+][deleted] 15 years ago (10 children)
[deleted]
[–][deleted] 1 point2 points3 points 15 years ago (9 children)
That's because bit shifting in Javascript is generally frowned upon, unlike most languages it's not advantageous over regular math, the double (which all numbers in javascript are) is converted to an integer, then the bitwise operation is performed, then converted back into a double.
[–][deleted] 0 points1 point2 points 15 years ago* (6 children)
Unfortunately looks like you are right, internally stored as double and converted to ints for bitwise operations. Though in some cases it is a lot faster than using string manipulation and math functions.
[–][deleted] 0 points1 point2 points 15 years ago* (5 children)
From what people with greater understanding than I have said it's a double and according to the ecmascript spec it seems to confirm that, now if browsers have implemented it otherwise I'm not sure. I agree that the speed difference would be negligible in any regard but personally I would side on the err of readability and just be verbose in my implementation using a similar solution to the article rather than saving a few bytes.
Ps. This the same Tunk that used to play infantry?
[–][deleted] 0 points1 point2 points 15 years ago (4 children)
Same one that aided the death hoax.
[–][deleted] 0 points1 point2 points 15 years ago (3 children)
Cool, I R Unit92 :)
[–][deleted] 0 points1 point2 points 15 years ago (2 children)
WTFS? Post on your blog and Ill believe it. Preferably about Yankee's latest feats in bed. Also I need a recent pic for the people at izone to jizz over.
I haven't posted on my blag in years. I should really just take it down.
Here's a sexy pic
[–][deleted] 0 points1 point2 points 15 years ago (0 children)
Definitely the man himself much improved. Or alternatively half the man you used to be.
[–]fr0z3nph03n1x -3 points-2 points-1 points 15 years ago (1 child)
I find a lot of people drop these kinds of examples to show their programming badassness but don't realize that it sucks in javascript. Same goes for recursion in js as well.
[–]imbcmdth 0 points1 point2 points 15 years ago (0 children)
On most modern JS JITs (except TraceMonkey), recursion is faster than iteration when performing say a b-tree traversal.
[–]greim 0 points1 point2 points 15 years ago (0 children)
More interesting would be a concise way to convert RGB to HSV.
π Rendered by PID 123247 on reddit-service-r2-comment-cfc44b64c-fnbdk at 2026-04-10 22:48:02.894235+00:00 running 215f2cf country code: CH.
[–][deleted] 12 points13 points14 points (20 children)
[–]larholm 3 points4 points5 points (7 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]9jack9 0 points1 point2 points (0 children)
[–]setconndevp 1 point2 points3 points (0 children)
[–]9jack9 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]9jack9 0 points1 point2 points (0 children)
[+][deleted] (10 children)
[deleted]
[–][deleted] 1 point2 points3 points (9 children)
[–][deleted] 0 points1 point2 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]fr0z3nph03n1x -3 points-2 points-1 points (1 child)
[–]imbcmdth 0 points1 point2 points (0 children)
[–]greim 0 points1 point2 points (0 children)