PHP Classes

Color Tools: Convert colors between different color models

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 127 All time: 9,399 This week: 455Up
Version License PHP version Categories
colortools 1.0.0BSD License5PHP 5, Graphics


This class can convert colors between different color models.

It can take a color parameter as a value in several color models or as a name and converts it any of the color models it supports.

Current it supports color models in RGB hexadecimal, RGB, HSL, HSB, WebSafe, CMY, CMYK, GRAYSCALE, an array of individual byte values or an integer value.

The names of supported colors are read from a text file.

Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 58
All time rank: 13710 in France France
Week rank: 13 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x



        This example shows the conversion some color values into different colorimetric


require_once ( 'Colors.phpclass' ) ;

    if (
php_sapi_name ( ) != 'cli' )
"<pre>" ;

$test_colors = array
) ;

    foreach (
$test_colors as $test_color )
"Color to test : $test_color\n" ;
$color = new Color ( $test_color ) ;
"Color value in different colorimetric models :" ;
"\t" . str_replace ( "\n", "\n\t", print_r ( $color -> ToArray ( ), true ) ) . "\n" ;



The Color class allows for conversion between different colorimetric models, namely :

  • RGB
  • HSL
  • HSB
  • CMY
  • CMYK
  • Gray scale
  • Websafe
  • or simply a color name, defined in the ColorNames.txt file supplied in this package


Instantianting a Color object will automatically convert the color you supplied to all the other colorimetric schemes :

$color 	=  new Color ( 'White' ) ;

The above example found the color you specified in the ColorNames.txt file, and converted this color name to all other colorimetric models currently support, which are accessible as properties ; for example :

$color -> RGB

is an array of three elements, containing the Red, Green and Blue parts of the color you supplied to the constructor (White), so it will contain the values 0xFF, 0xFF and 0xFF.

You can reuse an existing Color object to specify another color, using the SetColor() method :

$color -> SetColor ( '#0000FF' ) ;

The class will recognize the specified value to be "Blue", a value which will be accessible through the $ColorName property.

See the Reference section for more information about methods, properties and constants available for this class.




$color 	=  new Color ( $value, $model = null ) ;

Creates a Color object using the specified color value, which can have one of the following forms :

  • A color name, as defined in file *ColorNames.txt'
  • An HTML/CSS color specification, in the form #digits. Such RGB values can take various forms (ie, they can contain 3 or 6 digits). See the NormalizeRGBValue for a description of the various forms you can specify, and that will be normalized.
  • RGB(r,g,b) (or RVB) : Specifies an RGB value
  • HSL(h,s,l) (or TSL) : Color using the HSL model
  • HSB(h,s,b) (or HSV or TSV) : Color using the HSB model
  • WebSafe(r,g,b) (or WSafe or WSRGB or WRGB) : An RGB color that will be converted to a websafe color
  • CMY(c,m,y) (or CMJ) : Specifies a Cyan/Magenta/Yellow color value
  • CMYK(c,m,y,l) (or CMJN) : Specifies a Cyan/Magenta/Yellow/Black color value
  • GRAYSCALE(v) (or GSCALE or GS) : A grayscale color value.
  • An array of individual byte values
  • An integer value

Color names are not case-sensitive, as well as keywords such as RGB, GRAYSCALE, etc.

If a single integer or an array of bytes is specified, the caller must specify a constant of type COLOR\_SCHEME\_\* to indicate which colorimetric model is to be used for interpreting the value.


$result = Color::NormalizedRGBValue ( $value ) ;

Normalizes an RGB value by applying the following process :

  • Adds a leading '#' if none present
  • If exactly 3 characters are specified, the digits are doubled. For example, "#123" will yield to "#112233".
  • If less than 6 characters are specified, leading zeros are added.
  • Hex digits are converted to uppercase.

The function returns the normalized RGB value as a string, or false if an error occurred.


$color -> SetColor ( $value, $model = null ) ;

Assigns a new color to the specified object. All the colorimetric properties are recomputed after that.


$array 	=  $color -> ToArray ( ) ;

Returns an associative array having the following entries :

  • 'RGBString' : The HTML string corresponding to the color's RGB value.
  • 'ColorName' : Color name, if a match has been found.
  • 'RGB' : An array of three values, corresponding to the RGB colorimetric model.
  • 'HSL' : Same, for the HSL colorimetric model.
  • 'HSB' : Same, for the HSB colorimetric model.
  • 'CMY' : Same, for the CMY colorimetric model.
  • 'CMYK' : An array of four values, corresponding to the CMYK colorimetric model.
  • 'WebSafe' : An array of three RGB values, considered to be web-safe (ie, correctly rendered by most navigators).
  • 'GrayScale' : An array of three RGB values, corresponding to the associated color in the gray colorimetric model.


All the properties listed in the following section should be considered as read-only.


The normalized RGB color, that can be used as is for any css or HTML color attribute.


Human-readable color name. This value will be an empty string if the color has no correspondance in the file ColorNames.txt.


A set of 3 byte values for the RGB colorimetric model.


A set of 3 byte values for the HSL colorimetric model.


A set of 3 byte values for the HSB colorimetric model.


A set of 3 byte values for the CMY colorimetric model.


A set of 4 byte values for the CMYK colorimetric model.


A set of 3 byte values for the Websafe RGB colorimetric model.


A set of 3 byte values corresponding to the grayscale version of the underlying color.


The Color class defines the following constants, which indicate the colorimetric model associated to a string ; such a constant must be specified to either the class constructor or the SetValue() method, when the specified value does not allow to determine which colorimetric model is to be used :


  Files folder image Files  
File Role Description
Accessible without login Plain text file ColorNames.txt Data Documentation
Plain text file Colors.phpclass Class Class source
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,399
This week:455Up