How to Convert CMYK to RGB With PHP

Written by cathlene s. baptista
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert CMYK to RGB With PHP
PHP can convert CMYK print colours to RGB computer colour approximations. (Hemera Technologies/AbleStock.com/Getty Images)

The CMYK colour model is an additive model used in print. By adding percentages (0 to 100 per cent) of cyan, magenta, yellow and black, printers can "trick" the human eye into perceiving different colours. The RGB model is a subtractive colour model used by computers. By subtracting the values -- from 0 to 255 -- of red, blue and green, computers can "trick" the human eye into perceiving different colours. When you are creating Web pages, you can use PHP to convert CMYK colours to their RGB approximations for display on the Web.

Skill level:
Easy

Other People Are Reading

Things you need

  • Text editor
  • Web server with PHP installed and configured

Show MoreHide

Instructions

  1. 1

    Open a text editor and create a new file named CMYKtoRGB.php. Add a PHP open delimiter ("<?php") and a PHP close delimiter ("?>") to the file. These delimiters tell the PHP program installed on the Web server that any code placed between them should be interpreted as PHP code.

    <?php

    ?>

  2. 2

    Declare four PHP variables to hold the individual values for the CMYK colour that will be converted to RGB. Name the first variable "$C" (cyan) and give it a value of "1". Name the second variable "$M" (magenta) and give it a value of "0". Name the third value "$Y" (yellow) and give it a value of "0". Name the fourth value "$K" (black) and give it a value of "0". The individual CMYK colour values are now C=100%, M=0%, Y=0%, K=0%, or 100% cyan.

    <?php

    $C = 1;

    $M = 0;

    $Y = 0;

    $K = 0;

    ?>

  3. 3

    Use the PHP "echo" language construct to print the CMYK values to CMYKtoRGB.php. Leave a space before and after each string value (" C: ", " M: ", " Y: ", " K: ") and use the PHP concatenation operator (".") to print each variable's value ("$C, $M, $Y, $K") to the Web page.

    <?php

    $C = 1;

    $M = 0;

    $Y = 0;

    $K = 0;

    echo " C: " . $C;

    echo " M: " . $M;

    echo " Y: " . $Y;

    echo " K: " . $K;

    ?>

  4. 4

    Determine the "R" value of the "RGB" colour using the cyan ("$C") and black ("$K") values. The cyan value ("$C") is "1" and the black value ("$K") is "0". Store the result of the rounded equation ((1-1)(1-0)255) in a variable named "$R".

    <?php

    $C = 1;

    $M = 0;

    $Y = 0;

    $K = 0;

    echo " C: " . $C;

    echo " M: " . $M;

    echo " Y: " . $Y;

    echo " K: " . $K;

    $R = round(((1-$C)(1-$K))255);

    ?>

  5. 5

    Determine the "G" value of the "RGB" colour using the magenta ("$M") and black ("$K") values. The magenta value ("$M") is "0" and the black value ("$K") is "0". Store the result of the rounded equation ((1-0)(1-0)255) in a variable named "$G".

    <?php

    $C = 1;

    $M = 0;

    $Y = 0;

    $K = 0;

    echo " C: " . $C;

    echo " M: " . $M;

    echo " Y: " . $Y;

    echo " K: " . $K;

    $R = round(((1-$C)(1-$K))255);

    $G = round(((1-$M)(1-$K))255);

    ?>

  6. 6

    Determine the "B" value of the "RGB" colour using the yellow ("$Y") and black ("$K") values. The yellow value ("$Y") is "0" and the black value ("$K") is "0". Store the result of the rounded equation's ((1-0)(1-0)255) in a variable named "$B".

    <?php

    $C = 1;

    $M = 0;

    $Y = 0;

    $K = 0;

    echo " C: " . $C;

    echo " M: " . $M;

    echo " Y: " . $Y;

    echo " K: " . $K;

    $R = round(((1-$C)(1-$K))255);

    $G = round(((1-$M)(1-$K))255);

    $B = round(((1-$Y)(1-$K))255);

    ?>

  7. 7

    Use the PHP "echo" language construct to print the RGB values to CMYKtoRGB.php. Leave a space before and after each string value (" R: ", " G: ", " B: ") and use the PHP concatenation operator (".") to print each variable's value ("$R, $G, $B").

    <?php

    $C = 1;

    $M = 0;

    $Y = 0;

    $K = 0;

    echo " C: " . $C;

    echo " M: " . $M;

    echo " Y: " . $Y;

    echo " K: " . $K;

    $R = round(((1-$C)(1-$K))255);

    $G = round(((1-$M)(1-$K))255);

    $B = round(((1-$Y)(1-$K))255);

    echo " R: " . $R;

    echo " G: " . $G;

    echo " B: " . $B;

    ?>

  8. 8

    Open "CMYKtoRGB.php" in a Web browser. Verify that the RGB value is translated from the CMYK value to an approximate RGB value.

Tips and warnings

  • CMYK to RGB conversions can be done programmatically using software suites such as ImageMagick.
  • Commercial software programs, such as Adobe Photoshop and Adobe Illustrator, provide CMYK to RGB conversion tools, and may be more precise in some applications.
  • HTML and CSS use hexadecimal notation to display colour. RGB and CMYK colours can be converted to hexadecimal notation for colour approximation.
  • Colour conversions are extremely complex. There are many equations that can be used to convert CMYK colours to RGB colour, but they are all approximations.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.