I've defined this function in a .js file. It's supposed to take a color, make it the background color in a table cell, and adjust the color of the text depending on whether the color is dark or light. What is wrong with the code? function brighttest(season) { String fontColor = season; // remove hash character from string String rawFontColor = fontColor.substring(1,fontColor.length()); // convert hex string to int int rgb = Integer.parseInt(rawFontColor, 16); Color c = new Color(rgb); float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null); float brightness = hsb[2]; if (brightness < 0.5) { document.write('<td width="25%" bgcolor="'+season+'">'); document.write ('<font color="#FFF8C6">'+season+'</font>'); //prints the value } else { document.write('<td width="25%" bgcolor="'+season+'">'); document.write ('<font color="#000000">'+season+'</font>'); //prints the value } }
It causes all other links to images and text to not show up... and I'm sure that it is not the calling of the function itself, rather the code of the function that has some error. EDIT: I'm not 100% sure whether the copied part (everything before the if command) is in fact javascript or java. I copied most of this code online, which may be causing me to confuse javascrpit with Java (after considering jhine's response)... It is stated on the original site that this code "uses the java.awt.Color class to derive the brightness value of the colour". If this is in fact a java question, then I'd request the admin to move it to the appropriate forum.
Thats strange. You are using the java 'awt' libraries. Along with Javscript's 'document.write' function. If u want to do this in JS, here are a number of JS libraries which you can use to implement what u want. No need of coding it. eg: Colors.js, JSColor etc.. You can find a list from here http://www.queness.com/post/10081/11-javascript-color-pickers-plugins
The code is blowing up at the 3rd line: String fontColor = season; since that's not valid Javascript. Nothing after that will even be seen. If you're looking at this in IE you should see a notice at the bottom left corner of the browser that there's a problem. In Firefox with Firebug, you'll get a script error pointing at that line of code. You have to rewrite your code in Javascript. What you wrote, as jhine said, is Java, a totally different programming language (which won't run in web browsers).