fade a hex color - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

fade a hex color

owen1owen1 Posts: 76Member
does anybody know how to fade a hex color by a certain amount? e.g. I need a PHP function like fade('#FFAEFF', -10 ). I code use the rbg(0,0,0) but that only works in IE. I want to fade my tables.

[hr]
http://owensoft.20m.com
check out my current projects: http://www.owensoft.net

Comments

  • compuchipcompuchip Posts: 273Member
    Hint: increase all values (red AND blue AND green) with the same amount, which will give you a fade effect.
    For example:
    #989898 - Original: gray
    #A9A9A9 - Increased all by hex 11
    #EDEDED - Increased all by hex 44
    #FFFFFF - Finally, they become white

    Or decrease them if you want to fade to black.

    : does anybody know how to fade a hex color by a certain amount? e.g. I need a PHP function like fade('#FFAEFF', -10 ). I code use the rbg(0,0,0) but that only works in IE. I want to fade my tables.
    :
    : [hr]
    : http://owensoft.20m.com
    : check out my current projects: http://www.owensoft.net
    :

  • owen1owen1 Posts: 76Member
    [b][red]This message was edited by owen1 at 2003-1-22 13:49:54[/red][/b][hr]
    : Hint: increase all values (red AND blue AND green) with the same

    :x I know that. that's not the problem. the problem is increasing the hex value. You know like 1 + 1 = 2 but not with numbers with hex values. I need to know or a function that adds (or subtracts) #0000ff from #0AAAEE. Get what I am saying?
    [hr]
    http://owensoft.20m.com
    check out my current projects: http://www.owensoft.net



  • awulfawulf Posts: 40Member
    i wanted to do the same thing on my site at awulf.inversiondesigns.com for my calendar.

    here's the code i used.
    you input two hex colors, and the degree to which you want to fade between the two. here ya go...

    an example call to the function is
    $new_color = figureColor("#000000", "#FFFFFF", 5); will return a gray of some sort. play around with the value you send $count. i can't remember what its bounds are. but this code should do the trick.

    --------------------

    function figureColor($dark, $light, $count){
    // $dark and $light come in format "#FFFFFF" or something.
    // they have the # with 'em
    $dark = substr($dark, 1, 6);
    $light = substr($light, 1, 6);

    $offset = array();
    for($i = 0; $i <6; $i+=2){
    $offset[] = getOffset(substr($dark, $i, 2), substr($light, $i, 2), $count);
    }

    //rebuild the new color.
    $newColor = implode("", $offset);
    $newColor = "#" . $newColor;

    return $newColor;
    }

    function getOffSet($dark_in, $light_in, $count){
    // $dark_in and $light_in are only one color of the rgb
    // example for a color #FFCC99, the inputs would be sent in
    // as either a FF, CC, or 99.
    // returns the faded portion of either the color input
    $lighterAmount = 17;

    $dark = array();
    $dark[] = hexToInt(substr($dark_in,0,1));
    $dark[] = hexToInt(substr($dark_in,1,1));

    $light = array();
    $light[] = hexToInt(substr($light_in,0,1));
    $light[] = hexToInt(substr($light_in,1,1));

    $dark[0] *= 16;
    $light[0] *= 16;

    $dark = $dark[0] + $dark[1];
    $light = $light[0] + $light[1];

    $new = $light - $dark - $lighterAmount;


    $new = $new * $count/3;
    $new = intval($new);

    if($new < 0){
    $new = 0;
    }
    if($new > 255){
    $new = 255;
    }

    $new = $light - $new;

    $new = intToHex($new);

    return $new;
    }

    function hexToInt($ret){
    if(strtoupper($ret) == "F"){
    $ret = 15;
    }else
    if(strtoupper($ret) == "E"){
    $ret = 14;
    }else
    if(strtoupper($ret) == "D"){
    $ret = 13;
    }else
    if(strtoupper($ret) == "C"){
    $ret = 12;
    }else
    if(strtoupper($ret) == "B"){
    $ret = 11;
    }else
    if(strtoupper($ret) == "A"){
    $ret = 10;
    }

    return $ret;
    }

    function intToHex($ret){

    $hex = array();
    $one = $ret % 16;
    $ret -= $one;
    $hex[] = $ret / 16;
    $hex[] = $one;

    if($hex[0] < 0){
    $hex[0] = 0;
    }
    if($hex[1] < 0){
    $hex[1] = 0;
    }


    for($i = 0; $i < 2; $i++){
    if($hex[$i] == 15){
    $hex[$i] = "F";
    }else
    if($hex[$i] == 14){
    $hex[$i] = "E";
    }else
    if($hex[$i] == 13){
    $hex[$i] = "D";
    }else
    if($hex[$i] == 12){
    $hex[$i] = "C";
    }else
    if($hex[$i] == 11){
    $hex[$i] = "B";
    }else
    if($hex[$i] == 10){
    $hex[$i] = "A";
    }
    }


    $hex = implode("", $hex);

    return $hex;
    }


    --------------------



    : [b][red]This message was edited by owen1 at 2003-1-22 13:49:54[/red][/b][hr]
    : : Hint: increase all values (red AND blue AND green) with the same
    :
    : :x I know that. that's not the problem. the problem is increasing the hex value. You know like 1 + 1 = 2 but not with numbers with hex values. I need to know or a function that adds (or subtracts) #0000ff from #0AAAEE. Get what I am saying?
    : [hr]
    : http://owensoft.20m.com
    : check out my current projects: http://www.owensoft.net
    :
    :
    :
    :

  • owen1owen1 Posts: 76Member
    thanx, for you help
    it's a bit long but I'll figure it out
    [hr]
    http://owensoft.20m.com
    check out my current projects: http://www.owensoft.net

Sign In or Register to comment.