Color class implementation :S

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Color class implementation :S

Post by BETA »

Woah it's been a long time since my last post here... so hello everyone!
Well, let's get down to business :)
I've posted this in another php forum but had no replies and as I think this is a larger community and a good one... here it is :D

I'm making a Color class with conversions between different colorspaces, colors, etc. so it can be helpful later to use with image libraries, like GD or ImageMagick...
So I've come to a point where I don't know what to do. A lot of ways of implementing this class come to my head and I don't know which one would be the best to keep working on (I mean, it isn't finished and I don't want to write both, I'd like to choose one). So that's basically what I want you to help me with... to choose one of these two. The one you think would be better in every aspect especially when processing images.
Also if you find any errors, or have any ideas, anything... please let me know i will really appreciate it :D
Attached are the two implementations.
Below I post some basic examples of how to use each one but I think it's easy enough.

color.class.php:

Code: Select all

<?php
require_once("color.class.php");
 
print Color::rgb2hex(array(255,0,255),0,1);
print "<br/>";
 
$col = new Color;
$col->set_from_hex("#FF0000");
 
$col2 = new Color;
$col2->set_from_cmy(0, 1, 1);
 
print $col2->get_hex(1, 1);
print "<br/>";
?>
color.class2.php:

Code: Select all

<?php
require_once("color.class2.php");
$rgb1 = new rgbColor(255,0,0,255);
$rgb2 = new rgbColor(255,0,0,255);
$cmy = new cmyColor(0,1,1,1);
$cmyk = new cmykColor(0,1,1,0,1);
 
print $rgb1->equals($rgb2);
print "<br/>";
 
print $cmyk;
print "<br/>";
 
print ColorConverter::toCmyk($rgb1);
print "<br/>";
 
print $rgb1->getAlpha();
?>
Thanks in advance!

P.S: In case you aren't familiar with colorspaces... RGB components are in the range 0..255, CMY and CMYK in the range 0..1, and notice the second implementation has also alpha channel while the first one doesn't (just cause when I wrote it I didn't think about it... :P)
Attachments
class.rar
(3.37 KiB) Downloaded 3 times
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Color class implementation :S

Post by requinix »

The first one looks better to me, with a single Color class that can represent itself in different forms.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: Color class implementation :S

Post by BETA »

thx i really appreciate your comment :D, that's also what i thought...
then i'll wait to get some more comments to see which one to pick finally... :D
Thanks again! :)
Post Reply