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
- Wikipedia - Web Colors
- W3C - Techniques For Accessibility Evaluation And Repair Tools
- W3C - Techniques for WCAG 2.0
- Wikipedia - Color Models
- Color Conversion Formulas
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 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.
5 more comments by: Vincent de Lau, Jack Sleight, Matthew Ratzloff
ZF Home Page
Code Browser
Wiki Dashboard
This component would typically be used in the view to work with colors. So I think a view helper is a must have.