|
Wrote this after my 2-month break from the internet.
Figured out a way to shorten the Color object script without a hideous string parameter...here's a simplified version of that very code, Description: Darkens/brightens the hex code you provide. Syntax: convertHex( string, condition); Example: convertHex("0099CC", x + 2); The first parameter of the function takes the hexcode as a string. The second part uses the variable x to represent the color code as it's value is subtracted or added by any number. In this case we are increasing it's brightness by "2". The result will be: 22BBEE var validHex = "0123456789ABCDEF", x = 0; function convertHex() { var newHex = "", i = 0, x = 0; while(i < arguments[0].length) { var returnItem = validHex.charAt(validHex.indexOf(arguments[0].charAt(i ++)) + arguments[1]); newHex += returnItem ? returnItem : 0 + arguments[1] < 0 ? 0 : "F"; } return newHex; }
|
|
|
|
Nice job Aaron. You're right though, it's definitely much shorter then your last attempt. I might add parenthesis in the ternary operator, but that's it.
|
|
|
|
|
I was going to do something similar previously but didn't like the idea of a "increase", "decrease" argument through the function:
convertHex("0099CC", "increase", 2);
And as for the ternary operator, would there be any need for the parenthesis? It's on the other side of the assignment operator so there shouldn't be any problem.
|
|
|
|
I was going to do something similar previously but didn't like the idea of a "increase", "decrease" argument through the function: convertHex("0099CC", "increase", 2); And as for the ternary operator, would there be any need for the parenthesis? It's on the other side of the assignment operator so there shouldn't be any problem. Just wondering, why not use defined arguments?
|
|
|
|
As in what?
function convertHex(a, b)
|
|
|
|
As in what? function convertHex(a, b) Yeah, I see no point in using the arguments variable. You don't have variable variables, or unknown number of variables. It's really a set function so you might as well make it set.
|
|
|
|
I honestly don't know why. Maybe I just needed the typing practice.
|
|
|