Page 1 of 1

need help allocating string to a specific color

Posted: Thu Feb 25, 2010 5:29 am
by zchikmhagnet
Hello! a PHP newbie right here..

I need to allocate a string to a particular color..

I have a code but it only outputs a string assigned to white(255 255 255)

Code: Select all

    for ($x = 0; $x < $width; $x++) {
        for ($y = 0; $y < $height; $y++) {
 
        
            $colorIndex = ImageColorAt($img, $x, $height - 1 - $y);
            $color = ImageColorsForIndex($img, $colorIndex);
            $color['red']=intval((($color['red'])+15)/32)*32;    
            $color['green']=intval((($color['green'])+15)/32)*32;
            $color['blue']=intval((($color['blue'])+15)/32)*32;
           
            // symbol per color
         switch ($color) {
    case ($color['red'] = 255 and $color['green'] = 255 and $color['blue'] = 255) :
        $assignedsymbol = "00";
        break;
    case ($color['red'] = 148 and $color['green'] = 91 and $color['blue'] = 128) :
        $assignedsymbol = "01";
        break;
    case ($color['red'] = 206 and $color['green'] = 148 and $color['blue'] = 186) :
        $assignedsymbol = "02";
        break;
    case ($color['red'] = 000 and $color['green'] = 255 and $color['blue'] = 000) :
        $assignedsymbol = "03";
        break;
 
 
............
 default:
        $assignedsymbol == "0";
}
 
 
any other ideas or alternatives? they are greatly appreciated.. thanks!!

Re: need help allocating string to a specific color

Posted: Thu Feb 25, 2010 8:44 am
by AbraCadaver
This is not how a switch statement works. You are switching on the value of color, which I assume is a number or string, yet all of your case expressions evaluate to a boolean true. You must likely need to use IF statements instead, or redesign your switch correctly.

Re: need help allocating string to a specific color

Posted: Sun Feb 28, 2010 7:45 am
by zchikmhagnet
thanks!! I've done IF statements and it went out well..

Code: Select all

  if ($color['red'] == 255 && $color['green'] == 255 && $color['blue'] == 255) {
            $assignedsymbol = "00";
            } elseif ($color['red'] == 148 && $color['green'] == 91 && $color['blue'] == 128) {
            $assignedsymbol = "01";
            } elseif ($color['red'] == 206 && $color['green'] == 148 && $color['blue'] == 186) {
            $assignedsymbol = "02";
            }




thanks again