RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Convert Between RGB and CMYK Color Values with PHP

Convert Between RGB and CMYK Color Values with PHP

The CMYK color model is a subtractive color model used in color press printing. CMYK refers to the four inks used in some color printing: cyan, magenta, yellow, and key (black). In additive color models such as RGB (used in computer monitors, for example), white is the "additive" combination of all primary colored lights, while black is the absence of light. In the CMYK model, it is the opposite: inked (white) paper absorbs or reflects specific wavelengths, so cyan, magenta and yellow pigments serve as filters, subtracting varying degrees of red, green and blue from white light to produce a selective gamut of spectral colors (the CMYK color model is called subtractive because inks "subtract" brightness from white).

To save money on ink, and to produce deeper black tones, unsaturated and dark colors are produced by using black ink instead of the combination of cyan, magenta and yellow. The actual printing is made up of low(ish) resolution dots in a way that forces our brain to fill in the gaps and see an image. With this means of rendering images, photographers often whinge and whine about the way their photos are represented in print media because of the way in which CMYK printing deflates their vibrant colors... so it's not a perfect model, but it works well for general printing purposes.

Convert Between RGB and CMYK Color Values with PHP

Image: When CMY "primaries" are combined at full strength, the resulting "secondary" mixtures are red, green, and blue. Mixing all three gives black.

Read more about CMYK on Wikipedia .

The RGB model can represent every colour that CMYK can represent; but the converse is not true. Without tapping into ImageMagik or something similar, the values returned by a PHP function are "close" at best. The functions on this page will do an okay job of converting between RGB and CMYK values. If you come across a more effective means, please let me know.

RGB to CMYK

1
<?php 
2
/*
3
 Convert Between RGB and CMYK Color Values with PHP
4
 http://www.beliefmedia.com/convert-rgb-cmyk-color
5
*/
6
 
7
function beliefmedia_rgb_cmyk($r, $g, $b) {
8
  $c = (255 - $r) / 255.0 * 100;
9
  $m = (255 - $g) / 255.0 * 100;
10
  $y = (255 - $b) / 255.0 * 100;
11
 
12
  $b = min(array($c,$m,$y));
13
  $c = $c - $b; $m = $m - $b; $y = $y - $b;
14
 
15
  $cmyk = array( 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $b);
16
 return $cmyk;
17
}
18
 
19
/* Usage */
20
$r = '255'; $g = '166'; $b = '0';
21
echo '
22
<pre>' . print_r(beliefmedia_rgb_cmyk($r, $g, $b), true) . '</pre>
23
 
24
';

CMYK to RGB

1
<?php 
2
/*
3
 Convert Between RGB and CMYK Color Values with PHP
4
 http://www.beliefmedia.com/convert-rgb-cmyk-color
5
*/
6
 
7
function beliefmedia_cmyk_rgb($c, $m, $y, $k, $array = false, $format = 'rgb(%d, %d, %d)') {
8
  $c = $c / 100;
9
  $m = $m / 100;
10
  $y = $y / 100;
11
  $k = $k / 100;
12
 
13
  $r = 1 - ($c * (1 - $k)) - $k;
14
  $g = 1 - ($m * (1 - $k)) - $k;
15
  $b = 1 - ($y * (1 - $k)) - $k;
16
 
17
  $r = round($r * 255);
18
  $g = round($g * 255);
19
  $b = round($b * 255);
20
 
21
  $rgb = ($array) ? array('r'=>$r, 'g'=>$g, 'b'=>$b) : sprintf($format, $r, $g, $b);
22
 
23
 return $rgb;
24
}
25
 
26
/* Usage */
27
$c = '0'; $m = '35'; $y = '100'; $k = '0';
28
// echo beliefmedia_cmyk_rgb($c, $m, $y, $k);
29
echo 'pre' . print_r(beliefmedia_cmyk_rgb($c, $m, $y, $k, $array = true), true) . '/pre';

The function will return either a formatted string (alter the function format parameter as required) or as an array.

Considerations

All the examples on this page have omitted the pre tags for formatting purposes. The download includes working code. We have a number of color posts scheduled (and published) that'll work with color data in various ways; they will all be available via the color Tag: color tag.

Download


Title: Convert RGB and CMYK Color Values
Description: Convert Between RGB and CMYK Color Values with PHP
  Download • Version 0.2, 714.0B, zip, Category: PHP Code & Snippets

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment