numbers to colors, anyone with a solution?

Discussion in 'PHP' started by falcondriver, Aug 7, 2007.

  1. #1
    hi, i try to "colorize" some numeric values here.
    lets say 100 should be red (#FF0000), 0= yellow (#FFFF00) and -100 green (#00FF00), and anything in between shades of red (between 0 and 100) and green (between -100 and 0); anyone with an idea how to do this?
     
    falcondriver, Aug 7, 2007 IP
  2. Kommunicate

    Kommunicate Peon

    Messages:
    60
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you looking to convert from RGB to Hex and vice versa? If so, these functions might help.
     
    Kommunicate, Aug 7, 2007 IP
  3. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The colour system you describe is made up of three parts, Red, Green and Blue (the primary colours). Each colour is described by it's decimal proportion - so, red is 255 Red, 0 Green, 0 Blue. 255 decimal is FF in hexidecimal (ie. base 16 which probably means nothing to anyone younger than 30!)

    Anyway, that is why red is ff 00 00.

    White is achieved by mixing the other colours together - ff ff ff.

    If you want purple, you'd mix red and blue - ff 00 ff

    And all other shades can be achieved through varying the quantities.
     
    ecentricNick, Aug 8, 2007 IP
  4. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #4
    hu - anyone speaking my language? i know the rgb and hexadecimal system, i just wanted to know how to give a certain number x between a high (red) and low (green) value a proportional color representation. however, i solved it:
    function get_color($x, $min=-100, $max=100) {
    	if($x<$min) {
    		$x=$min;
    	}
    	if($x>$max) {
    		$x=$max;
    	}
    	$range = abs($min)+abs($max);
    	$red = abs(($max+$x)*255)/$range;
    	$grn = abs(($min+$x)*255)/$range;
        $r = substr("00".dechex($red), -2);
        $g = substr("00".dechex($grn), -2);
    	$color = '#'.$r.$g.'00';
    	return $color;
    }
    PHP:
    ... and then i get the value from my spam-o-meter, connect it to the database and get: (see attachment).

    EXCELLENT! and the approve/delete buttom for this waiting queue is on the other side of this table :)
     

    Attached Files:

    falcondriver, Aug 8, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well pardon me for freely giving my time and trying to help :eek:)

    Anyway, your solution is overly complicated.

    If you have values between -100 and 100, want negatives to be red, and positives to be green, utilising the maximum colour range available, all you have to do is multiply by 2.55.

    
    function get_color($x, $min=-100, $max=100) {
        if($x<$min) {
            $x=$min;    
        }
        if($x>$max) {
            $x=$max;    
        }
        if ($x<0){
          $r=dechex($x*-2.55);
          $g='00';
        }else{
          $r='00';
          $g=dechex($x*2.55);
        }
        $color = '#'.$r.$g.'00';    
        return $color;
    }
    
    PHP:
     
    ecentricNick, Aug 8, 2007 IP
  6. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #6
    yeah, in this example. but $min and $max is a var and could also be 50 or 2000, so i have to use

    abs(($max+$x)*255)/$range;

    right?
     
    falcondriver, Aug 8, 2007 IP