Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question PHP function to subtract 2 from variables

    Hi guys

    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 is what I have so far for testing:

    $color1 = "1";
    $color1_1 = $color1 - 2;

    function filter_rgb ($hex){
    if($hex < 0){$hex = 0;}
    echo $hex;
    }

    filter_rgb($color1_1);


    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.

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hy,
    Try use the function from this example (no need to create 6 variables):
    Code:
    function lowerNrs($hex) {
      $re = '';
      for($i=0; $i<strlen($hex); $i++) {
        $hex[$i] = intval($hex[$i]);
        if($hex[$i] < 2) $hex[$i] = 0;
        else $hex[$i] = $hex[$i] - 2;
        $re .= $hex[$i];
      }
      return $re;
    }
    $hex = '123456';
    echo lowerNrs($hex);        // 001234

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    PHP can do hex math natively. Just prefix the hex value with 0x when doing the math. Example:
    PHP Code:
    // Add hex 32 to A1
    echo 0x32 0xa1;

    // Another example
    echo 0x3b3b3b 0xa1a1a1

  • Users who have thanked kbduvall for this post:

    brainbuzzmedia (06-04-2012)

  • #5
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ah yes I think this is just what I needed.

  • #6
    New Coder
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Np

  • #7
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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 02:40 AM.

  • #8
    New Coder
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    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.

  • Users who have thanked kbduvall for this post:

    brainbuzzmedia (06-14-2012)

  • #9
    New Coder
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #10
    New Coder
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    PHP Code:
    $var 0xe;
    if ((
    $var 0x2) > 0xf) { $var 0xf; }
    else { 
    $var $var 0x2; } 
    Etc.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •