<?php
/**************************************************
 ColorConversion.Class.php
 --------------------------------------------------
 Author:MS-K  http://till-daylight.org
 2008-05-30(金)

 ■留意事項■
 ■クラス化してのプログラムは初めてのものです。
 ■実装にダサい点やバギーな点が多々あるはずです。
 ■ダメ出し・突っ込みは心から歓迎いたします。
 ■各所でparent::__construct();していますが、本当に
 ■これでいいんだろうか…
 ■http://blog.till-daylight.org/archives/209-CMYK-RGB-Hex.html にて連絡可能です。
 --------------------------------------------------
 requirement PHP Version 5以上
 2010-07-21(水)追記:5.3以降ではstaticエラーが出ます…
 --------------------------------------------------
 使い方
 require './ColorConversion.Class.php';
 後は説明できるほど詳しくないので、index.phpを参照
 してください。
 --------------------------------------------------
 * 注意事項:
 個人での利用・改造は自由です。
 再配布は禁止します。
***************************************************/

/**
 * 入力色を代入する
 */
class InputColor {

    protected 
$c 0;
    protected 
$m 0;
    protected 
$y 0;
    protected 
$k 0;

    protected 
$r 0;
    protected 
$g 0;
    protected 
$b 0;

    protected 
$hex;

    protected 
$hex_r;
    protected 
$hex_g;
    protected 
$hex_b;

    public 
$errors = array();

    public function 
__construct() {
        if(isset(
$_POST['color'])) {
            switch(
$_POST['color']) {
            case 
'cmyk':
                
$this->$this->setCMYK($_POST['c']);
                
$this->$this->setCMYK($_POST['m']);
                
$this->$this->setCMYK($_POST['y']);
                
$this->$this->setCMYK($_POST['k']);
            break;

            case 
'rgb':
                
$this->$this->setRGB($_POST['r']);
                
$this->$this->setRGB($_POST['g']);
                
$this->$this->setRGB($_POST['b']);
            break;

            case 
'hex':
                
$this->hex $this->setHex($_POST['hex']);
            break;

            case 
'light':
                if(isset(
$_POST['rate'])) {
                    switch(
$_POST['rate']) {
                    case 
'10':
                    case 
'5':
                        
$rate intval($_POST['rate']);
                    break;
                    }
                }
                
$this->$this->setCMYK($_POST['dc']) - $rate;
                if(
$this->0$this->0;
                
$this->$this->setCMYK($_POST['dm']) - $rate;
                if(
$this->0$this->0;
                
$this->$this->setCMYK($_POST['dy']) - $rate;
                if(
$this->0$this->0;
                
$this->$this->setCMYK($_POST['dk']) - $rate;
                if(
$this->0$this->0;
            break;

            case 
'dark':
                if(isset(
$_POST['rate'])) {
                    switch(
$_POST['rate']) {
                    case 
'10':
                    case 
'5':
                        
$rate intval($_POST['rate']);
                    break;
                    }
                }
                
                if(
$_POST['dc'] !== 0) {
                    
$this->$this->setCMYK($_POST['dc']) + $rate;
                    if(
$this->100$this->100;
                } else {
                    
$this->10;
                }
                if(
$_POST['dm'] !== 0) {
                    
$this->$this->setCMYK($_POST['dm']) + $rate;
                    if(
$this->100$this->100;
                } else {
                    
$this->10;
                }
                if(
$_POST['dy'] !== 0) {
                    
$this->$this->setCMYK($_POST['dy']) + $rate;
                    if(
$this->100$this->100;
                } else {
                    
$this->10;
                }
                if(
$_POST['dk'] !== 0) {
                    
$this->$this->setCMYK($_POST['dk']) + $rate;
                    if(
$this->100$this->100;
                } else {
                    
$this->10;
                }
            break;
            }
        }
    }


    
/**
     * 入力値のチェック
     */
    
private function setCMYK($inputdata) {
        
$inputdata trim($inputdata);
        if(
is_numeric($inputdata) and $inputdata >=and $inputdata <=100
            
or empty($inputdata)) {
            return 
intval($inputdata);
        } else {
            
$this->errors[] = '0から100までの数値のみ有効です。<br />0を代入しました。';
            return 
0;
        }
    }

    private function 
setRGB($inputdata) {
        
$inputdata trim($inputdata);
        if(
is_numeric($inputdata) and $inputdata >=and $inputdata <=255
            
or empty($inputdata)) {
            return 
intval($inputdata);
        } else {
            
$this->errors[] = '0から255までの数値のみ有効です。<br />0を代入しました。';
            return 
0;
        }
    }

    private function 
setHex($inputdata) {
        
$inputdata trim($inputdata);
        
        if(!
eregi("[^0-9a-f]"$inputdata)) {
            
// 6桁または6桁以上の場合、6桁を返す
            
if(strlen($inputdata) >= 6) {
                
$inputdata substr($inputdata06);
                
// 小文字で統一
                
return strtolower($inputdata);

            
// 3桁の場合、各桁を繰り返す
            
} elseif(strlen($inputdata) == 3) {
                
$inputdata substr($inputdata01).substr($inputdata01).
                            
substr($inputdata11).substr($inputdata11).
                            
substr($inputdata21).substr($inputdata21);
                return 
strtolower($inputdata);
            
// 不足している桁を0で埋めて6桁を返す(空白時も)
            
} else {
                
$inputdata sprintf("%-06s"$inputdata);
                return 
$inputdata;
            }
        } else {
            
$this->errors[] = '0から9までの数値とaからfまでのアルファベットのみ有効です。<br />ffffffを代入しました。';      return 'ffffff';
        }
    }

    private function 
errorMes() {
        if(
count($this->errors) > 0) {
            return 
$this->errors;
        } else {
            return 
FALSE;
        }
    }
}


/**
 * CMYKをRBGに変換する
 */
class CMYK2RGB extends InputColor {
    
    public function 
__construct() {
        
parent::__construct();

        
$this->$this->100;
        
$this->$this->100;
        
$this->$this->100;
        
$this->$this->100;
        
        
$this->round((1min(1$this->* (1$this->k) + $this->k))*255);
        
$this->round((1min(1$this->* (1$this->k) + $this->k))*255);
        
$this->round((1min(1$this->* (1$this->k) + $this->k))*255);
    }

    public function 
showRGB() {
        
$line $this->.','$this->.','$this->b;
        return 
$line;
    }
}


/**
 * CMYKをHEXに変換する
 */
class CMYK2Hex extends CMYK2RGB {
    public function 
__construct() {
        
CMYK2RGB::__construct();
        
        
$this->hex_r sprintf("%02s",dechex($this->r));
        
$this->hex_g sprintf("%02s",dechex($this->g));
        
$this->hex_b sprintf("%02s",dechex($this->b));
    }

    public function 
showHex() {
        
$line $this->hex_r $this->hex_g $this->hex_b;
        return 
$line;
    }
}


/**
 * RBGをCMYKに変換する
 */
class RGB2CMYK extends InputColor {
    public function 
__construct() {
        
parent::__construct();

        
$tmpr $this->255;
        
$tmpg $this->255;
        
$tmpb $this->255;
        
$tmpk min($tmpr$tmpg$tmpb);

        
// R0G0B0、#000000(黒)の時
        
if($tmpr === and $tmpg === and $tmpb === 1) {
            
$tmpk 0;
        }
        
        
$this->round(($tmpr $tmpk) / ($tmpk) * 100);
        
$this->round(($tmpg $tmpk) / ($tmpk) * 100);
        
$this->round(($tmpb $tmpk) / ($tmpk) * 100);
        
$this->floor($tmpk);
    }

    public function 
showCMYK() {
        
$line $this->.','$this->.','$this->.','$this->k;
        return 
$line;
    }
}


/**
 * RBGを16進に変換する
 */
class RGB2Hex extends InputColor {
    
    public function 
__construct() {
        
parent::__construct();

        
$this->hex_r sprintf("%02s",dechex($this->r));
        
$this->hex_g sprintf("%02s",dechex($this->g));
        
$this->hex_b sprintf("%02s",dechex($this->b));
    }

    public function 
showHex() {
        
$line $this->hex_r $this->hex_g $this->hex_b;
        return 
$line;
    }
}


/**
 * 16進をRBGに変換する
 */
class Hex2RGB extends InputColor {

    public function 
__construct() {
        
parent::__construct();

        
$hextmp unpack("A2rr/A2gg/A2bb"$this->hex);
        
$this->hexdec($hextmp['rr']);
        
$this->hexdec($hextmp['gg']);
        
$this->hexdec($hextmp['bb']);
    }

    public function 
showHex() {
        
$line $this->.','$this->.','$this->b;
        return 
$line;
    }
}


/**
 * 16進をCMYKに変換する
 */
class Hex2CMYK extends Hex2RGB {

    public function 
__construct() {
        
parent::__construct();
        
        
// 2010-07-21(水)追記:5.3以降ではstaticエラーが出ます…
        
RGB2CMYK::__construct();
    }

    public function 
showCMYK() {
        
$line $this->.','$this->.','$this->.','$this->k;
        return 
$line;
    }
}


/**
 * 入力値を出力する
 */
class PutData extends InputColor {
    public function 
putCMYK() {
        
$inputs $this->.','$this->.','$this->.','$this->k;
        return 
$inputs;
    }

    public function 
putRGB() {
        
$inputs $this->.','$this->.','$this->b;
        return 
$inputs;
    }

    public function 
putHEX() {
        
$inputs $this->hex;
        return 
$inputs;
    }
}