Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use CMYK on web page

I need to use CMYK colors on my web page. Is there any way to use CMYK in CSS or may be convert CMYK to RGB using JavaScript?

EDIT:
I mean I have colors creating algorithm in CMYK notation and I need to use it on web page.

like image 709
Sergey Metlov Avatar asked Jan 15 '12 11:01

Sergey Metlov


People also ask

Can you use CMYK for web?

Fundamentally, RGB is best for websites and digital communications, while CMYK is better for print materials. Most design fields recognize RGB as the primary colors, while CMYK is a subtractive model of color.

Can you use CMYK in HTML?

CMYK is not supported in HTML, but it is suggested as a new standard in CSS4.

What color mode is best for web?

As a quick reference, the RGB color mode is best for digital work, while CMYK is used for print products.

Can I use CMYK in CSS?

There's no way to use CMYK in CSS. You can either use RGB or HSL (CSS3 only). Here's a JavaScript algorithm to convert CMYK to RGB (and the other way around).


2 Answers

In the CSS Color Module Level 4 of the W3C as of 5 November 2019, there is a function called device-cmyk that can be used to define a device dependent CMYK color value.

Example:

color: device-cmyk(0 81% 81% 30%);

The function returns an RGB value that the device calculates by trying to convert the CMYK color to an RGB value that matches the CMYK color as close as possible.

Note: I can't find anything regarding the browser support. I guess that no browser is currently supporting this.

like image 167
Krisztián Balla Avatar answered Sep 28 '22 04:09

Krisztián Balla


There is no perfect algorithmic way to convert CMYK to RGB. CYMK is a subtractive color system, RGB is an additive color system. Each have different gamuts, which means there are colors that just cannot be represented in the other color system and vice versa. Both are device dependent color spaces, which really means that what color you really get is dependent on which device you use to reproduce that color, which is why you have color profiles for each device that adjust how it produces color into something more "absolute".

The best that you can do is approximate a simulation of one space onto the other. There is an entire field of computer science that is dedicated to this kind of work, and its non-trivial.

If you are looking for a heuristic for doing this, then the link that Cyrille provided is pretty simple math, and easily invertible to accept a CYMK color and produce a reasonable RGB facsimile.

A very simple heuristic is to map cyan to 0x00FFFF, magenta to 0xFF00FF, and yellow to 0xFFFF00, and black (key) to 0x000000. Then do something like this:

function cmykToRGB(c,m,y,k) {

    function padZero(str) {
        return "000000".substr(str.length)+str
    }

    var cyan = (c * 255 * (1-k)) << 16;
    var magenta = (m * 255 * (1-k)) << 8;
    var yellow = (y * 255 * (1-k)) >> 0;

    var black = 255 * (1-k);
    var white = black | black << 8 | black << 16;

    var color = white - (cyan | magenta | yellow );

    return ("#"+padZero(color.toString(16)));


}

invoking cmykToRGB with cmyk ranges from 0.0 to 1.0. That should give you back an RGB color code. But again this is just a heuristic, an actual conversation between these color spaces is much more complicated and takes into account a lot more variables then are represented here. You mileage may vary, and the colors you get out of this might not "look right"

jsFiddle here

like image 38
J. Holmes Avatar answered Sep 28 '22 05:09

J. Holmes