I am trying to figure out a way to split up a hexidecimal rgb color code and lower each digit by 2. I am using substr to split the color code into 6 variables: $color1_1, $color1_2 etc.
What I can't figure out is how to make a function that basically filters a number. It would lower it by 1, but if this results in the number being -1 then it would set it to equal 0. I know how to do this for one specific hard coded variable, but I need to do it for a ton of them. Each color code has 6 parts and I have about a dozen different color codes that need to be filtered.
This echos $color1_1 correctly as 0 inside the function, but it doesn't change the value of the actual variable $color1_1 after the function is run. I can't echo each part of the color code by running a function. I need it to either overwrite the value of the variable I ran the function with or set a new variable will somehow be unique and named similarly to whichever variable I used the function on.
Sorry if I am not explaining this too well, I am not an experienced php coder at all so I thank anyone for their help.
Well I really need to change several things depending on the variable rather than just this one. I'm not sure what all of that means in order to customize it but here is everything that I would need to change about a number.
start with six digit color code, i have to alter this for a bunch of different variations. for example, i need one variable where i lower each digit by 1. another where i lower by 2, another where i raise by 1, raise by 2 ect.
then if any digit would result in being less than 0 it would equal 0
if any digit would result in 10 it would equal a, 11 would equal b, 12 would equal c, 13 would equal d, 14 would equal e, and 15 would equal f.
Those would be all the conditions that would need to be changed, but there are about twelve different ways the original code needs to be modified so I am sort of unsure which would be the best way of doing this with the least amount of code. I was thinking one set of code for each of the twelve numbers I need to modify, and then another that is just like a filter to fix anything that is below 0 or above 9. The reason I broke them up into 6 individual numbers is because I thought it would get too confusing if a number went from one digit to two.
Question, if I need to add 0x to the beginning of the number how do I add/subtract using variables? My variable might be $red_1 and equal 1. How do I write $newred_1 = $red_1 - 0x1; while having the 0x in the first part of the equation?
Or wait, am I dumb and that just works with the variable without adding 0x to it.
Last edited by brainbuzzmedia; 06-04-2012 at 01:40 AM..
PHP is loosely typed, meaning it will convert types to make expressions "make sense". You can just do something like:
PHP Code:
$newred_1 = "0x{$red_1}" - 0x1;
And PHP will convert the string to a hex value before doing the math. I'm not sure how it will behave if you do something like "1 - 0x1". Off the top of my head I'm not sure if PHP will convert the integer to hex, or the other way around.
Is there any way to handle when you add to a hex that is already F so that it still is F instead of breaking? Likewise the same thing when subtracting below 0 to still equal 0 instead of a negative number.