PHP Classes

File: color_converter.php

Recommend this page to a friend!
  Classes of Usman Didi Khamdani   Color Harmony Class   color_converter.php   Download  
File: color_converter.php
Role: Example script
Content type: text/plain
Description: this example is used to to convert HEX, RGB and HSL color codes, one to anothers
Class: Color Harmony Class
Convert and generate harmonic colors
Author: By
Last change:
Date: 14 years ago
Size: 3,587 bytes
 

Contents

Class file image Download
<!--
this example is used to to convert HEX, RGB and HSL color codes, one to anothers
author: usman didi khamdani
author's email: usmankhamdani@gmail.com
author's phone: +6287883919293
-->

<h3>Color Converter</h3>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input<?php if(!isset($_POST['type']) || (isset($_POST['type']) && $_POST['type']=='HEX')) { echo ' checked'; } else {} ?> type="radio" name="type" value="HEX" /> HEX <input<?php if(isset($_POST['type']) && $_POST['type']=='RGB') { echo ' checked'; } else {} ?> type="radio" name="type" value="RGB" /> RGB <input<?php if(isset($_POST['type']) && $_POST['type']=='HSL') { echo ' checked'; } else {} ?> type="radio" name="type" value="HSL" /> HSL <input type="text" name="value" value="<?php if(isset($_POST['value'])) { echo $_POST['value']; } else {} ?>" /> <input type="submit" name="convert_color" value="Convert" /><br />
<ul>
<li>if the type of color is HEX, use double hexadecimal format for each Red, Green and Blue color, i.e ff0033 (ff for Red, 00 for Green and 33 for Blue)<br />
<i>for each Red, Green and Blue color, minimal value is 00 and maximal value is ff</i></li>
<li>if the type of color is RGB, separate each Red, Green and Blue color with comma, i.e 192,192,192<br />
<i>for each Red, Green and Blue color, minimal value is 0 and maximal value is 255</i></li>
<li>if the type of color is HSL, separate each Hue, Saturation and Lightness color with comma, i.e 300,90,40<br />
<i>for Hue color, minimal value is 0 and maximal value is 360. for each Saturation and Lightness color, minimal value is 0 and maximal value is 100</i></li>
</ul>
</form>

<hr />

<?php

include('color_converter.class.php');

$c = new colorConverter;

if(isset(
$_POST['value']) && $_POST['value']!=NULL) {

    if(
$_POST['type']=='HEX') {

       
$HEX = $_POST['value'];

       
$RGB = $c->HEX2RGB($HEX);
       
$HSL = $c->HEX2HSL($HEX);
       
       
$title = 'HEX & RGB';
       
$color1 = 'HEX => '.$HEX;
       
$color2 = 'RGB = >'.round($RGB[0]).','.round($RGB[1]).','.round($RGB[2]);
       
$color3 = 'HSL = >'.round($HSL[0]).','.round($HSL[1]).','.round($HSL[2]);

    }

    if(
$_POST['type']=='RGB') {

       
$RGB = explode(',',$_POST['value']);

       
$HEX = $c->RGB2HEX($RGB[0],$RGB[1],$RGB[2]);
       
$HSL = $c->RGB2HSL($RGB[0],$RGB[1],$RGB[2]);

       
$title = 'RGB & HEX';
       
$color1 = 'RGB => '.round($RGB[0]).','.round($RGB[1]).','.round($RGB[2]);
       
$color2 = 'HEX = >'.$HEX;
       
$color3 = 'HSL = >'.round($HSL[0]).','.round($HSL[1]).','.round($HSL[2]);

    }

    if(
$_POST['type']=='HSL') {

       
$HSL = explode(',',$_POST['value']);

       
$HEX = $c->HSL2HEX($HSL[0],$HSL[1],$HSL[2]);
       
$RGB = $c->HSL2RGB($HSL[0],$HSL[1],$HSL[2]);

       
$title = 'HSL & HEX';
       
$color1 = 'HSL => '.round($HSL[0]).','.round($HSL[1]).','.round($HSL[2]);
       
$color2 = 'HEX = >'.$HEX;
       
$color3 = 'RGB = >'.round($RGB[0]).','.round($RGB[1]).','.round($RGB[2]);

    }

    if((isset(
$c->HEXError) && $c->HEXError==1) || (isset($c->RGBError) && $c->RGBError==1) || (isset($c->HSLError) && $c->HSLError==1)) {

        if(
$_POST['type']=='HEX') {
            echo
$c->HEXErrorMessage;
        }
        if(
$_POST['type']=='RGB') {
            echo
$c->RGBErrorMessage;
        }
        if(
$_POST['type']=='HSL') {
            echo
$c->HSLErrorMessage;
        }
       
    } else {

?>

<table cellpadding="5" cellspacing="0" border="1">
<tr>
    <td>
        <b><?php echo $title; ?></b><br />
        <br />
        Base Color = <?php echo $color1; ?><br />
        Transform Color 1 = <?php echo $color2; ?><br />
        Transform Color 2 = <?php echo $color3; ?>
</td>
</tr>
</table>

<?php } } ?>