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


Quick Links:


newBookmarkLockedFalling

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
Wrote this after my 2-month break from the internet. :P



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;
}






Chris

Chris Avatar

******
Head Coder

19,519


June 2005
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. :)

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
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.

Eric

Eric Avatar



1,442


November 2005
derfleurer said:
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?

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
As in what?

function convertHex(a, b)

Eric

Eric Avatar



1,442


November 2005
derfleurer said:
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.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
I honestly don't know why. Maybe I just needed the typing practice. :P

newBookmarkLockedFalling