Skip to main content

CMYK to RGB Color

The CMYK to RGB color conversion is a common task in graphics and image processing. CMYK stands for Cyan, Magenta, Yellow, and Key (Black), and RGB stands for Red, Green, and Blue. CMYK is often used in color printing, while RGB is used for electronic displays like computer monitors and TVs. Converting from CMYK to RGB allows us to translate colors between these two color spaces.

Problem Statement

Given the CMYK values (Cyan, Magenta, Yellow, and Key) of a color, the task is to convert them into their corresponding RGB values. This conversion involves translating color intensities from one color space to another, ensuring that the resulting colors are visually accurate.

Example

Let's consider the first test case from your code:

  • C = 71
  • M = 48
  • Y = 60
  • K = 35

We want to convert these CMYK values to RGB.

Idea to Solve the Problem

The conversion from CMYK to RGB involves a series of mathematical calculations. We need to normalize the CMYK values to the range [0, 1], perform the calculations, and then convert the resulting RGB values back to the integer range [0, 255]. The main formulas used are based on subtractive color mixing for CMYK and additive color mixing for RGB.

Pseudocode

c1 = c / 100
m1 = m / 100
y1 = y / 100
k1 = k / 100

r = round((1 - c1) * (1 - k1) * 255)
g = round((1 - m1) * (1 - k1) * 255)
b = round((1 - y1) * (1 - k1) * 255)

Algorithm Explanation

  1. Normalize the CMYK values by dividing each component by 100.
  2. Calculate the intermediate values c1, m1, y1, and k1.
  3. Use the formulas to calculate the corresponding RGB values:
    • For Red (R): (1 - c1) * (1 - k1) * 255
    • For Green (G): (1 - m1) * (1 - k1) * 255
    • For Blue (B): (1 - y1) * (1 - k1) * 255
  4. Round the calculated RGB values to the nearest integers.
  5. Display the calculated RGB values.

Code Solution

// C program 
// CMYK to RGB Color
#include <stdio.h>

#include <math.h>

// Convert CMYK to RGB
void cmyk_to_rgb(int c, int m, int y, int k)
{
    if (c < 0 || m < 0 || y < 0 || k < 0)
    {
        // Invalid parameter
        return;
    }
    printf("\n Given CMYK");
    printf("\n C = %d ", c);
    printf("\n M = %d ", m);
    printf("\n Y = %d ", y);
    printf("\n K = %d \n", k);
    double c1 = c / 100.0;
    double m1 = m / 100.0;
    double y1 = y / 100.0;
    double k1 = k / 100.0;
    int r = round((1 - c1) *(1 - k1) *255);
    int g = round((1 - m1) *(1 - k1) *255);
    int b = round((1 - y1) *(1 - k1) *255);
    // Display Calculate results
    printf("\n Calculate RGB");
    printf("\n R = %d ", r);
    printf("\n G = %d ", g);
    printf("\n B = %d ", b);
}
int main()
{
    // Test Case
    cmyk_to_rgb(71, 48, 60, 35);
    cmyk_to_rgb(24, 66, 32, 42);
    return 0;
}

Output

 Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66
 Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101
/*
  Java Program
  CMYK to RGB color
*/
public class ColorConversion
{
    // Convert CMYK to RGB
    public void cmyk_to_rgb(int c, int m, int y, int k)
    {
        if (c < 0 || m < 0 || y < 0 || k < 0)
        {
            return;
        }
        System.out.print("\n Given CMYK");
        System.out.print("\n C = " + c);
        System.out.print("\n M = " + m);
        System.out.print("\n Y = " + y);
        System.out.print("\n K = " + k + " \n");
        double c1 = c / 100.0;
        double m1 = m / 100.0;
        double y1 = y / 100.0;
        double k1 = k / 100.0;
        int r = (int) Math.round((1 - c1) * (1 - k1) * 255);
        int g = (int) Math.round((1 - m1) * (1 - k1) * 255);
        int b = (int) Math.round((1 - y1) * (1 - k1) * 255);
        // Display Calculate results
        System.out.print("\n Calculate RGB");
        System.out.print("\n R = " + r);
        System.out.print("\n G = " + g);
        System.out.print("\n B = " + b);
    }
    public static void main(String[] args)
    {
        ColorConversion color = new ColorConversion();
        // Test Case
        color.cmyk_to_rgb(71, 48, 60, 35);
        color.cmyk_to_rgb(24, 66, 32, 42);
    }
}

Output

 Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66
 Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101
// Include header file
#include <iostream>
#include <math.h>

using namespace std;
/*
  C++ Program
  CMYK to RGB color
*/
class ColorConversion
{
    public:
        // Convert CMYK to RGB
        void cmyk_to_rgb(int c, int m, int y, int k)
        {
            if (c < 0 || m < 0 || y < 0 || k < 0)
            {
                return;
            }
            cout << "\n Given CMYK";
            cout << "\n C = " << c;
            cout << "\n M = " << m;
            cout << "\n Y = " << y;
            cout << "\n K = " << k << " \n";
            double c1 = c / 100.0;
            double m1 = m / 100.0;
            double y1 = y / 100.0;
            double k1 = k / 100.0;
            int r = (int) round((1 - c1) * (1 - k1) *255);
            int g = (int) round((1 - m1) * (1 - k1) *255);
            int b = (int) round((1 - y1) * (1 - k1) *255);
            // Display Calculate results
            cout << "\n Calculate RGB";
            cout << "\n R = " << r;
            cout << "\n G = " << g;
            cout << "\n B = " << b;
        }
};
int main()
{
    ColorConversion color = ColorConversion();
    // Test Case
    color.cmyk_to_rgb(71, 48, 60, 35);
    color.cmyk_to_rgb(24, 66, 32, 42);
    return 0;
}

Output

 Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66
 Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101
// Include namespace system
using System;
/*
  C# Program
  CMYK to RGB color
*/
public class ColorConversion
{
    // Convert CMYK to RGB
    public void cmyk_to_rgb(int c, int m, int y, int k)
    {
        if (c < 0 || m < 0 || y < 0 || k < 0)
        {
            return;
        }
        Console.Write("\n Given CMYK");
        Console.Write("\n C = " + c);
        Console.Write("\n M = " + m);
        Console.Write("\n Y = " + y);
        Console.Write("\n K = " + k + " \n");
        double c1 = c / 100.0;
        double m1 = m / 100.0;
        double y1 = y / 100.0;
        double k1 = k / 100.0;
        int r = (int) Math.Round((1 - c1) * (1 - k1) * 255);
        int g = (int) Math.Round((1 - m1) * (1 - k1) * 255);
        int b = (int) Math.Round((1 - y1) * (1 - k1) * 255);
        // Display Calculate results
        Console.Write("\n Calculate RGB");
        Console.Write("\n R = " + r);
        Console.Write("\n G = " + g);
        Console.Write("\n B = " + b);
    }
    public static void Main(String[] args)
    {
        ColorConversion color = new ColorConversion();
        // Test Case
        color.cmyk_to_rgb(71, 48, 60, 35);
        color.cmyk_to_rgb(24, 66, 32, 42);
    }
}

Output

 Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66
 Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101
<?php
/*
  Php Program
  CMYK to RGB color
*/
class ColorConversion
{
    // Convert CMYK to RGB
    public  function cmyk_to_rgb($c, $m, $y, $k)
    {
        if ($c < 0 || $m < 0 || $y < 0 || $k < 0)
        {
            return;
        }
        echo "\n Given CMYK";
        echo "\n C = ". $c;
        echo "\n M = ". $m;
        echo "\n Y = ". $y;
        echo "\n K = ". $k ." \n";
        $c1 = ($c / 100.0);
        $m1 = ($m / 100.0);
        $y1 = ($y / 100.0);
        $k1 = ($k / 100.0);
        $r =  round((1 - $c1) * (1 - $k1) * 255);
        $g =  round((1 - $m1) * (1 - $k1) * 255);
        $b =  round((1 - $y1) * (1 - $k1) * 255);
        // Display Calculate results
        echo ord("\n Calculate RGB");
        echo "\n R = ". $r;
        echo "\n G = ". $g;
        echo "\n B = ". $b;
    }
}

function main()
{
    $color = new ColorConversion();
    // Test Case
    $color->cmyk_to_rgb(71, 48, 60, 35);
    $color->cmyk_to_rgb(24, 66, 32, 42);
}
main();

Output

 Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35
10
 R = 48
 G = 86
 B = 66
 Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42
10
 R = 112
 G = 50
 B = 101
/*
  Node Js Program
  CMYK to RGB color
*/
class ColorConversion
{
    // Convert CMYK to RGB
    cmyk_to_rgb(c, m, y, k)
    {
        if (c < 0 || m < 0 || y < 0 || k < 0)
        {
            return;
        }
        process.stdout.write("\n Given CMYK");
        process.stdout.write("\n C = " + c);
        process.stdout.write("\n M = " + m);
        process.stdout.write("\n Y = " + y);
        process.stdout.write("\n K = " + k + " \n");
        var c1 = (c / 100.0);
        var m1 = (m / 100.0);
        var y1 = (y / 100.0);
        var k1 = (k / 100.0);
        var r = parseInt(Math.round((1 - c1) * (1 - k1) * 255));
        var g = parseInt(Math.round((1 - m1) * (1 - k1) * 255));
        var b = parseInt(Math.round((1 - y1) * (1 - k1) * 255));
        // Display Calculate results
        process.stdout.write("\n Calculate RGB");
        process.stdout.write("\n R = " + r);
        process.stdout.write("\n G = " + g);
        process.stdout.write("\n B = " + b);
    }
}

function main()
{
    var color = new ColorConversion();
    // Test Case
    color.cmyk_to_rgb(71, 48, 60, 35);
    color.cmyk_to_rgb(24, 66, 32, 42);
}
main();

Output

 Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66
 Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101
#   Python 3 Program
#   CMYK to RGB color

class ColorConversion :
    #  Convert CMYK to RGB
    def cmyk_to_rgb(self, c, m, y, k) :
        if (c < 0 or m < 0 or y < 0 or k < 0) :
            return
        
        print(" Given CMYK\n", end = "")
        print(" C = ", c )
        print(" M = ", m )
        print(" Y = ", y )
        print(" K = ", k ," \n")
        c1 = (c / 100.0)
        m1 = (m / 100.0)
        y1 = (y / 100.0)
        k1 = (k / 100.0)
        r = int(round((1 - c1) * (1 - k1) * 255))
        g = int(round((1 - m1) * (1 - k1) * 255))
        b = int(round((1 - y1) * (1 - k1) * 255))
        #  Display Calculate results
        print(" Calculate RGB")
        print(" R = ", r )
        print(" G = ", g )
        print(" B = ", b,"\n" )
    

def main() :
    color = ColorConversion()
    #  Test Case
    color.cmyk_to_rgb(71, 48, 60, 35)
    color.cmyk_to_rgb(24, 66, 32, 42)

if __name__ == "__main__": main()

Output

 Given CMYK
 C =  71
 M =  48
 Y =  60
 K =  35

 Calculate RGB
 R =  48
 G =  86
 B =  66

 Given CMYK
 C =  24
 M =  66
 Y =  32
 K =  42

 Calculate RGB
 R =  112
 G =  50
 B =  101
#   Ruby Program
#   CMYK to RGB color

class ColorConversion 
    #  Convert CMYK to RGB
    def cmyk_to_rgb(c, m, y, k) 
        if (c < 0 || m < 0 || y < 0 || k < 0) 
            return
        end

        print("Given CMYK\n")
        print(" C = ", c ,"\n")
        print(" M = ", m ,"\n")
        print(" Y = ", y ,"\n")
        print(" K = ", k ," \n\n")
        c1 = c / 100.0
        m1 = m / 100.0
        y1 = y / 100.0
        k1 = k / 100.0
        r = (((1 - c1) * (1 - k1) * 255).round).to_i
        g = (((1 - m1) * (1 - k1) * 255).round).to_i
        b = (((1 - y1) * (1 - k1) * 255).round).to_i
        #  Display Calculate results
        print(" Calculate RGB\n")
        print(" R = ", r ,"\n")
        print(" G = ", g ,"\n")
        print(" B = ", b ,"\n\n")
    end

end

def main() 
    color = ColorConversion.new()
    #  Test Case
    color.cmyk_to_rgb(71, 48, 60, 35)
    color.cmyk_to_rgb(24, 66, 32, 42)
end

main()

Output

Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35 

 Calculate RGB
 R = 48
 G = 86
 B = 66

Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42 

 Calculate RGB
 R = 112
 G = 50
 B = 101

/*
  Scala Program
  CMYK to RGB color
*/
class ColorConversion
{
    // Convert CMYK to RGB
    def cmyk_to_rgb(c: Int, m: Int, y: Int, k: Int): Unit = {
        if (c < 0 || m < 0 || y < 0 || k < 0)
        {
            return;
        }
        print("Given CMYK\n");
        print(" C = " + c + "\n");
        print(" M = " + m + "\n");
        print(" Y = " + y + "\n");
        print(" K = " + k + " \n\n");
        var c1: Double = (c / 100.0);
        var m1: Double = (m / 100.0);
        var y1: Double = (y / 100.0);
        var k1: Double = (k / 100.0);
        var r: Int = (Math.round((1 - c1) * (1 - k1) * 255)).toInt;
        var g: Int = (Math.round((1 - m1) * (1 - k1) * 255)).toInt;
        var b: Int = (Math.round((1 - y1) * (1 - k1) * 255)).toInt;
        // Display Calculate results
        print(" Calculate RGB\n");
        print(" R = " + r + "\n");
        print(" G = " + g + "\n");
        print(" B = " + b + "\n\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var color: ColorConversion = new ColorConversion();
        // Test Case
        color.cmyk_to_rgb(71, 48, 60, 35);
        color.cmyk_to_rgb(24, 66, 32, 42);
    }
}

Output

Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66

Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101
import Foundation
/*
  Swift 4 Program
  CMYK to RGB color
*/
class ColorConversion
{
    // Convert CMYK to RGB
    func cmyk_to_rgb(_ c: Int, _ m: Int, _ y: Int, _ k: Int)
    {
        if (c < 0 || m < 0 || y < 0 || k < 0)
        {
            return;
        }
        print("Given CMYK");
        print(" C = ", c);
        print(" M = ", m);
        print(" Y = ", y);
        print(" K = ", k, " \n");
        let c1: Double = Double(c) / 100.0;
        let m1: Double = Double(m) / 100.0;
        let y1: Double = Double(y) / 100.0;
        let k1: Double = Double(k) / 100.0;
        let r: Int = Int(round((1 - c1) * (1 - k1) * 255));
        let g: Int = Int(round((1 - m1) * (1 - k1) * 255));
        let b: Int = Int(round((1 - y1) * (1 - k1) * 255));
        // Display Calculate results
        print(" Calculate RGB");
        print(" R = ", r);
        print(" G = ", g);
        print(" B = ", b, "\n");
    }
}
func main()
{
    let color: ColorConversion = ColorConversion();
    // Test Case
    color.cmyk_to_rgb(71, 48, 60, 35);
    color.cmyk_to_rgb(24, 66, 32, 42);
}
main();

Output

Given CMYK
 C =  71
 M =  48
 Y =  60
 K =  35

 Calculate RGB
 R =  48
 G =  86
 B =  66

Given CMYK
 C =  24
 M =  66
 Y =  32
 K =  42

 Calculate RGB
 R =  112
 G =  50
 B =  101
/*
  Kotlin Program
  CMYK to RGB color
*/
class ColorConversion
{
    // Convert CMYK to RGB
    fun cmyk_to_rgb(c: Int, m: Int, y: Int, k: Int): Unit
    {
        if (c<0 || m<0 || y<0 || k<0)
        {
            return;
        }
        print("Given CMYK\n");
        print(" C = " + c + "\n");
        print(" M = " + m + "\n");
        print(" Y = " + y + "\n");
        print(" K = " + k + " \n\n");
        var c1: Double = c / 100.0;
        var m1: Double = m / 100.0;
        var y1: Double = y / 100.0;
        var k1: Double = k / 100.0;
        var r: Int =  Math.round((1 - c1) * (1 - k1) * 255).toInt();
        var g: Int =  Math.round((1 - m1) * (1 - k1) * 255).toInt();
        var b: Int =  Math.round((1 - y1) * (1 - k1) * 255).toInt();
        // Display Calculate results
        print(" Calculate RGB\n");
        print(" R = " + r + "\n");
        print(" G = " + g + "\n");
        print(" B = " + b + "\n\n");
    }
}
fun main(args: Array<String>): Unit
{
    var color: ColorConversion = ColorConversion();
    // Test Case
    color.cmyk_to_rgb(71, 48, 60, 35);
    color.cmyk_to_rgb(24, 66, 32, 42);
}

Output

Given CMYK
 C = 71
 M = 48
 Y = 60
 K = 35

 Calculate RGB
 R = 48
 G = 86
 B = 66

Given CMYK
 C = 24
 M = 66
 Y = 32
 K = 42

 Calculate RGB
 R = 112
 G = 50
 B = 101

Resultant Output Explanation

For the first test case (71, 48, 60, 35):

  • Normalized CMYK values: c1 = 0.71, m1 = 0.48, y1 = 0.6, k1 = 0.35
  • Calculated RGB values: r = 48, g = 86, b = 66

For the second test case (24, 66, 32, 42):

  • Normalized CMYK values: c1 = 0.24, m1 = 0.66, y1 = 0.32, k1 = 0.42
  • Calculated RGB values: r = 112, g = 50, b = 101

Time Complexity

The time complexity of this algorithm is constant, O(1), because the calculations involve only a fixed number of arithmetic operations regardless of the input values. The rounding operation and print statements also don't affect the overall complexity.

Finally

In this article, we've discussed the problem of converting CMYK color values to RGB color values. We explained the concept, provided suitable examples, presented the algorithmic approach, and discussed the time complexity. This article aims to simplify the understanding of the CMYK to RGB color conversion process and provide readers with a clear explanation of how the conversion is performed.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment