Added by Jack Sleight, last edited by Jack Sleight on Jun 11, 2008  (view change)

Labels

 

Zend Framework: Zend_Color Component Proposal

Proposed Component Name Zend_Color
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend_Color
Proposers Jack Sleight
Revision 0.1 - 10 July 2007
0.2 - 21 July 2007
0.2.1 - 21 July 2007
0.3 - 22 February 2008 (wiki revision: 34)

Table of Contents

1. Overview

Zend_Color is a component for generating colours, manipulating colour values, and converting colours between different colour models. Typical uses include using this within views, where you want elements to appear in different tints of a colour based on a dynamic variable, or even customising CSS dynamically to apply custom colour schemes. You could also use the WCAG functions to validate user input colour values offer enough contrast, in for example advertising blocks.

The current version can be downloaded here: http://jacksleight.svn.beanstalkapp.com/zend_color/trunk/

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will provide components for RGB, HSL, HSV (HSB) and CMYK colour models.
  • This component will not provide a component for "HEX colours", as "HEX" is not a separate colour model, it is simply an RGB colour expressed in Hexadecimal.
  • This component will provide helper methods within the RGB component for easily working with "HEX colours", and named colours in the HTML spec/X11.
  • This component will support conversion between RGB to/from HSL, RGB to/from HSV and RGB to/from CMYK.
  • This component will provide a consistent API for manipulating colour values, including setting and retrieving colour values in decimal, percentages, fractions and degrees.
  • This component will provide helper functions for generating CSS compatible colour unit strings.
  • This component will provide methods to make a colour web safe or web smart, and to test for WCAG 1.0 and 2.0 contrast compliance.

4. Dependencies on Other Framework Components

  • Zend_Exception

5. Theory of Operation

Typically the four colour model classes (Rgb, Hsl, Hsv and Cmyk) are instantiated as objects, and then those objects each represent a colour. The colour objects can be modified by altering the values (e.g. R, G and B), or can be used to generate the same (or similar) colour in another colour model. The Rgb class contains two static factory methods for generating Rgb objects from HEX colour strings, and HTML spec/X11 colour names. The base class (Zend_Color) only contains the constants required by the other classes.

When a colour object is instantiated the user can specify which format (range, percentages, fractions or degrees) the colour values are in (supported formats vary between colour models). In addition, each colour value object has methods for setting/retrieving and adjusting values in any of the supported formats. Although colour values can be specified, retrieved and adjusted in multiple formats, they are always stored as fractions (between 0 and 1) within the value objects. The Rgb object also contains methods for retrieving the HEX value of a colour, and the closest named colour from the HTML spec/X11 list.

6. Milestones / Tasks

  • Milestone 1: [DONE] Finalise initial proposal ready for community review
  • Milestone 2: Working prototype checked into the incubator
  • Milestone 3: Unit tests exist, work, and are checked into SVN
  • Milestone 4: Initial documentation exists

7. Class Index

  • Zend_Color_Exception
  • Zend_Color
  • Zend_Color_Cmyk
  • Zend_Color_Hsl
  • Zend_Color_Hsv
  • Zend_Color_Rgb
  • Zend_Color_Value
  • Zend_Color_Value_Hue
  • Zend_Color_Value_Rgb

8. Use Cases

UC-01

Get RGB object from hex code

UC-02

Create an RGB object

UC-03

Create an RGB object using percentages

UC-04

Get the RGB values

UC-05

Make a colour web safe or web smart

UC-06

Find the closest named HTML colour

UC-07

Check W3C visibility

UC-08

Convert RGB to HSL, HSV or CMYK

UC-09

Convert HSL, HSV or CMYK to RGB

UC-10

Modify colour values

9. Class Skeletons

This component would typically be used in the view to work with colors. So I think a view helper is a must have.

Hi Lars,
Yes, you're right, the typical use for this component would be in views. However as colour manipulations involve direct calls to the colour objects, the only view helpers you could rally have would be to instantiate new colour objects, which you could do without the need for a helper.

This could be helpfull. My only problem is that you always convert trought RGB, which can't cover the complete colorspectrum. You might consider converting using a device independent model, for instance CIE XYZ or CIE L*ab.

Introducing device independent convesion allows for usage outside the computerscreen domain, for instance print (CMY and CMYK) and video (YIQ, YUV and co.).

Hi Vincent,
I agree that including a device independent model such as CIE XYZ would be a welcome addition, and I had intended to include this in a future version, however at the time of writing I didn't have enough time to work on additional colour models. I'll look into this, and if feasible for the first version I'll add it in.

The reason for the focus RGB at present is because this would most commonly used for web sites, which obviously use RGB almost exclusively (although HSL is also supported in CSS).

Might I suggest to add a base class (Zend_Color_Abstract?), which enforces a toLab(), fromLab() and a toRgb()?

This is a very rough scetch and I'm not sure if this would cover everything. Adding a base class, would however ensure a common ground for conversion. You could use CIE XYZ instead of CIE L*ab of course.

Yes, good idea. I'll add it in.

View the rest of this thread. Most recent comment: Jun 16, 2008
5 more comments by: Vincent de Lau, Jack Sleight, Matthew Ratzloff

Curious, would it be possible to add some color theory functions?

  • getComplementaryColor()
  • getAnalogousColor()
  • getTriadicColor()

Is Jack still working on this? Else I'm going to fork it to a new proposal with a (imho) better API, Color Profile handling, etc.

Hi Ben,
I'm sorry to say that with my current workload I'm not going to be able to continue working on this for the foreseeable future. You're welcome to take anything from here, as well as the code from http://jacksleight.svn.beanstalkapp.com/zend_color/trunk/ to use as a starting point if you want. You may also want to contact Vincent de Lau (vincent [at] delau [dot] nl), as he was very keen to (and did) contribute code to the proposal, and may be interested in contributing further.