Hey I was wondering if anybody knew a way to generate some monochromatic colors based off a single color inputted in hex into a javascript function.
views:
91answers:
3
+1
Q:
Does anybody know how to generate 4 colors with new brightnesss based of a color in javascript?
+1
A:
Try:
// color looks like 0xRRGGBB
function(color){
R = color.substr(2, 2)
G = color.substr(4, 2)
B = color.substr(6, 2)
return "0x" + R + R + R
// or return ["0x"+R+R+R, "0x"+G+G+G, "0x"+B+B+B]
}
By the way: "monochromatic color" is an oxymoron, isn't it? ;-)
Dejw
2010-02-16 00:19:06
+3
A:
If you're looking to convert a color to some level of gray scale, try calculating the luminescence, like this:
function toGrayScale(color) /* color is an integer */ {
// Extract the red, green, and blue portions of the color
var red = (color >> 16) & 0xff;
var green = (color >> 8) & 0xff;
var blue = color & 0xff;
// Calculate the luminescence
var luminescence = red * 0.3 + green * 0.59 + blue * 0.11;
var lumInt = Math.floor(luminescence);
// Combine into a grayscale color
return (lumInt << 16) + (lumInt << 8) + lumInt;
}
Jacob
2010-02-16 00:32:32
Can you explain the 0.3, 0.59, 0.11 values please?
meouw
2010-02-16 00:47:29
Hm, I was wondering about the Kuler-like monochromatic, where the colors "focus on varied lumicity and intensity while keeping the hue." Is this a HSV question?
Kyle
2010-02-16 00:49:40
@meouw, different applications use different formulas. http://www.gamedev.net/community/forums/topic.asp?topic_id=448456 has a different set of numbers, where R = 0.2126, G = 0.7152, B = 0.0722. It's subjective how to best map color to gray scale.
Jacob
2010-02-16 01:05:45
+2
A:
What you want to do is calculate the relative value for each color component against the average luminescence, then multiply each of these by a pair of offsets to generate different brightness...
This is pseudo-code, I'm sure you can figure out the details:
function getColors(baseline) {
var offsets = [ 0x33, 0x66, 0x99, 0xCC ];
// Use Jake's suggestion on computing luminescence...
var lum = getLuminescence(baseline);
var redCoefficient = baseline[red] / lum;
var greenCoefficient = baseline[green] / lum;
var blueCoefficient = baseline[blue] / lum;
var output = [];
for (offsetInd in offsets) {
var offset = offsets[offsetInd];
output.push(new Color(offset * redCoefficient,
offset * greenCofficient, offset * blueCoefficient));
}
return ouptut;
}
levik
2010-02-16 00:52:01
I'm sorry, I'm new to raw Javascript, what are the colors being inputed as? An array?
Kyle
2010-02-16 01:09:13
Sorry, as I said - this is pseudo code - assumes you can figure out how to get the red/green/blue component of the baseline color, and compute luminescence - the other answers show you how to do this.
levik
2010-02-16 01:12:59
After a ton of hacking, this one works! Thanks to everybody for the help though, I did use all the code =D
Kyle
2010-02-16 01:39:40
Kyle - can you post your final solution so others (like me) don't have to go through the ton of hacking you did?
mlissner
2010-08-02 23:30:30