Posted on by Kalkicode
Code Conversion

RGB to CMYK color

The RGB to CMYK color conversion is a common task in graphic design and printing. RGB stands for Red, Green, and Blue, while CMYK stands for Cyan, Magenta, Yellow, and Key (Black). RGB is primarily used for electronic displays like computer monitors, TVs, and screens, while CMYK is used for color printing. Converting RGB colors to CMYK colors is important to ensure accurate color reproduction across different media.

Problem Statement

Given the RGB values (Red, Green, and Blue) of a color, the task is to convert them into their corresponding CMYK values. This conversion involves translating color intensities from the RGB color space to the CMYK color space.

Example

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

  • R = 181
  • G = 122
  • B = 230

We want to convert these RGB values to CMYK.

Idea to Solve the Problem

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

Pseudocode

r1 = r / 255
g1 = g / 255
b1 = b / 255

black = 1 - max(r1, max(g1, b1))

if black != 1:
    c = round(100 * (1 - r1 - black) / (1 - black))
    m = round(100 * (1 - g1 - black) / (1 - black))
    y = round(100 * (1 - b1 - black) / (1 - black))
k = round(black * 100)

Algorithm Explanation

  1. Normalize the RGB values by dividing each component by 255.
  2. Calculate the normalized values r1, g1, and b1.
  3. Determine the "black" value, which represents the amount of black ink needed. It is calculated as 1 minus the maximum of r1, g1, and b1.
  4. If black is not equal to 1, calculate the CMY values using formulas that take into account the black component:
    • Cyan (C): round(100 * (1 - r1 - black) / (1 - black))
    • Magenta (M): round(100 * (1 - g1 - black) / (1 - black))
    • Yellow (Y): round(100 * (1 - b1 - black) / (1 - black))
  5. Calculate the black component (Key) as: round(black * 100)
  6. Display the calculated CMYK values.

Code Solution

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

// Returns the maximum value of given two numbers
double max_value(double a, double b)
{
    if (a > b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
// Convert RGB to CMYK
void rgb_to_cmyk(int r, int g, int b)
{
    if (r < 0 || g < 0 || b < 0)
    {
        return;
    }
    printf("\n Given RGB");
    printf("\n R = %d ", r);
    printf("\n G = %d ", g);
    printf("\n B = %d ", b);
    // Calculate R' G' B'
    double r1 = r / 255.0;
    double g1 = g / 255.0;
    double b1 = b / 255.0;
    // Black key 
    double black = (1 - max_value(max_value(r1, g1), b1));
    int c = 0;
    int m = 0;
    int y = 0;
    int k = 0;
    if (black != 1)
    {
        // Cyan color 
        c = round(100 *(1 - r1 - black) / (1 - black));
        // Magenta color 
        m = round(100 *(1 - g1 - black) / (1 - black));
        // Yellow color
        y = round(100 *(1 - b1 - black) / (1 - black));
    }
    // Black color
    k = round(black *100);
    // Display Calculate results
    printf("\n Calculate CMYK");
    printf("\n C = %d ", c);
    printf("\n M = %d ", m);
    printf("\n Y = %d ", y);
    printf("\n K = %d \n", k);
}
int main()
{
    // Test Case
    rgb_to_cmyk(181, 122, 230);
    rgb_to_cmyk(78, 37, 78);
    return 0;
}

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
/*
  Java Program
  RGB to CMYK color
*/
public class ColorConversion
{
    // Returns the maximum value of given two numbers
    public double max_value(double a, double b)
    {
        if (a > b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    // Convert RGB to CMYK
    public void rgb_to_cmyk(int r, int g, int b)
    {
        if (r < 0 || g < 0 || b < 0)
        {
            return;
        }
        System.out.print("\n Given RGB");
        System.out.print("\n R = " + r );
        System.out.print("\n G = " + g );
        System.out.print("\n B = " + b );
        // Calculate R' G' B'
        double r1 = r / 255.0;
        double g1 = g / 255.0;
        double b1 = b / 255.0;
        // Black key 
        double black = (1 - max_value(max_value(r1, g1), b1));
        long c = 0;
        long m = 0;
        long y = 0;
        long k = 0;
        if (black != 1)
        {
            // Cyan color 
            c = Math.round(100 * ((1 - r1 - black) / (1 - black)));
            // Magenta color 
            m = Math.round(100 * ((1 - g1 - black) / (1 - black)));
            // Yellow color
            y = Math.round(100 * ((1 - b1 - black) / (1 - black)));
        }
        // Black color
        k = Math.round(black * 100);
        // Display Calculate results
        System.out.print("\n Calculate 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");
    }
    public static void main(String[] args)
    {
        ColorConversion color = new ColorConversion();
        // Test Case
        color.rgb_to_cmyk(181, 122, 230);
        color.rgb_to_cmyk(78, 37, 78);
    }
}

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
// Include header file
#include <iostream>
#include <math.h>
using namespace std;

/*
  C++ Program
  RGB to CMYK color
*/

class ColorConversion
{
    public:
        // Returns the maximum value of given two numbers
        double max_value(double a, double b)
        {
            if (a > b)
            {
                return a;
            }
            else
            {
                return b;
            }
        }
    // Convert RGB to CMYK
    void rgb_to_cmyk(int r, int g, int b)
    {
        if (r < 0 || g < 0 || b < 0)
        {
            return;
        }
        cout << "\n Given RGB";
        cout << "\n R = " << r;
        cout << "\n G = " << g;
        cout << "\n B = " << b;
        // Calculate R' G' B'
        double r1 = r / 255.0;
        double g1 = g / 255.0;
        double b1 = b / 255.0;
        // Black key
        double black = (1 - this->max_value(this->max_value(r1, g1), b1));
        long c = 0;
        long m = 0;
        long y = 0;
        long k = 0;
        if (black != 1)
        {
            // Cyan color
            c = round(100 *((1 - r1 - black) / (1 - black)));
            // Magenta color
            m = round(100 *((1 - g1 - black) / (1 - black)));
            // Yellow color
            y = round(100 *((1 - b1 - black) / (1 - black)));
        }
        // Black color
        k = round(black *100);
        // Display Calculate results
        cout << "\n Calculate CMYK";
        cout << "\n C = " << c;
        cout << "\n M = " << m;
        cout << "\n Y = " << y;
        cout << "\n K = " << k << " \n";
    }
};
int main()
{
    ColorConversion color = ColorConversion();
    // Test Case
    color.rgb_to_cmyk(181, 122, 230);
    color.rgb_to_cmyk(78, 37, 78);
    return 0;
}

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
// Include namespace system
using System;
/*
  C# Program
  RGB to CMYK color
*/
public class ColorConversion
{
    // Returns the maximum value of given two numbers
    public double max_value(double a, double b)
    {
        if (a > b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    // Convert RGB to CMYK
    public void rgb_to_cmyk(int r, int g, int b)
    {
        if (r < 0 || g < 0 || b < 0)
        {
            return;
        }
        Console.Write("\n Given RGB");
        Console.Write("\n R = " + r);
        Console.Write("\n G = " + g);
        Console.Write("\n B = " + b);
        // Calculate R' G' B'
        double r1 = r / 255.0;
        double g1 = g / 255.0;
        double b1 = b / 255.0;
        // Black key
        double black = (1 - max_value(max_value(r1, g1), b1));
        int c = 0;
        int m = 0;
        int y = 0;
        int k = 0;
        if (black != 1)
        {
            // Cyan color
            c = (int) Math.Round(100 * ((1 - r1 - black) / (1 - black)));
            // Magenta color
            m = (int) Math.Round(100 * ((1 - g1 - black) / (1 - black)));
            // Yellow color
            y = (int) Math.Round(100 * ((1 - b1 - black) / (1 - black)));
        }
        // Black color
        k = (int) Math.Round(black * 100);
        // Display Calculate results
        Console.Write("\n Calculate CMYK");
        Console.Write("\n C = " + c);
        Console.Write("\n M = " + m);
        Console.Write("\n Y = " + y);
        Console.Write("\n K = " + k + " \n");
    }
    public static void Main(String[] args)
    {
        ColorConversion color = new ColorConversion();
        // Test Case
        color.rgb_to_cmyk(181, 122, 230);
        color.rgb_to_cmyk(78, 37, 78);
    }
}

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
<?php
/*
  Php Program
  RGB to CMYK color
*/
class ColorConversion
{
    // Returns the maximum value of given two numbers
    public
    function max_value($a, $b)
    {
        if ($a > $b)
        {
            return $a;
        }
        else
        {
            return $b;
        }
    }
    // Convert RGB to CMYK
    public
    function rgb_to_cmyk($r, $g, $b)
    {
        if ($r < 0 || $g < 0 || $b < 0)
        {
            return;
        }
        echo "\n Given RGB";
        echo "\n R = ".$r;
        echo "\n G = ".$g;
        echo "\n B = ".$b;
        // Calculate R' G' B'
        $r1 = ($r / 255.0);
        $g1 = ($g / 255.0);
        $b1 = ($b / 255.0);
        // Black key
        $black = (1 - $this->max_value($this->max_value($r1, $g1), $b1));
        $c = 0;
        $m = 0;
        $y = 0;
        $k = 0;
        if ($black != 1)
        {
            // Cyan color
            $c = round(100 * (1 - $r1 - $black) / (1 - $black));
            // Magenta color
            $m = round(100 * (1 - $g1 - $black) / (1 - $black));
            // Yellow color
            $y = round(100 * (1 - $b1 - $black) / (1 - $black));
        }
        // Black color
        $k = round($black * 100);
        // Display Calculate results
        echo "\n Calculate CMYK";
        echo "\n C = ".$c;
        echo "\n M = ".$m;
        echo "\n Y = ".$y;
        echo "\n K = ".$k.
        " \n";
    }
}

function main()
{
    $color = new ColorConversion();
    // Test Case
    $color->rgb_to_cmyk(181, 122, 230);
    $color->rgb_to_cmyk(78, 37, 78);
}
main();

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
/*
  Node Js Program
  RGB to CMYK color
*/
class ColorConversion
{
    // Returns the maximum value of given two numbers
    max_value(a, b)
    {
        if (a > b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    // Convert RGB to CMYK
    rgb_to_cmyk(r, g, b)
    {
        if (r < 0 || g < 0 || b < 0)
        {
            return;
        }
        process.stdout.write("\n Given RGB");
        process.stdout.write("\n R = " + r);
        process.stdout.write("\n G = " + g);
        process.stdout.write("\n B = " + b);
        // Calculate R' G' B'
        var r1 = (r / 255.0);
        var g1 = (g / 255.0);
        var b1 = (b / 255.0);
        // Black key
        var black = (1 - this.max_value(this.max_value(r1, g1), b1));
        var c = 0;
        var m = 0;
        var y = 0;
        var k = 0;
        if (black != 1)
        {
            // Cyan color
            c = Math.round(100 * (1 - r1 - black) / (1 - black));
            // Magenta color
            m = Math.round(100 * (1 - g1 - black) / (1 - black));
            // Yellow color
            y = Math.round(100 * (1 - b1 - black) / (1 - black));
        }
        // Black color
        k = Math.round(black * 100);
        // Display Calculate results
        process.stdout.write("\n Calculate 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");
    }
}

function main()
{
    var color = new ColorConversion();
    // Test Case
    color.rgb_to_cmyk(181, 122, 230);
    color.rgb_to_cmyk(78, 37, 78);
}
main();

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
#   Python 3 Program
#   RGB to CMYK color

class ColorConversion :
    #  Returns the maximum value of given two numbers
    def max_value(self, a, b) :
        if (a > b) :
            return a
        else :
            return b
        
    
    #  Convert RGB to CMYK
    def rgb_to_cmyk(self, r, g, b) :
        if (r < 0 or g < 0 or b < 0) :
            return
        
        print("\n Given RGB", end = "")
        print("\n R =", r, end = "")
        print("\n G =", g, end = "")
        print("\n B =", b, end = "")
        #  Calculate R' G' B'
        r1 = (r / 255.0)
        g1 = (g / 255.0)
        b1 = (b / 255.0)
        #  Black key 
        black = (1 - self.max_value(self.max_value(r1, g1), b1))
        c = 0
        m = 0
        y = 0
        k = 0
        if (black != 1) :
            #  Cyan color 
            c = round(100 * (((1 - r1 - black) / (1 - black))))
            #  Magenta color 
            m = round(100 * (((1 - g1 - black) / (1 - black))))
            #  Yellow color
            y = round(100 * (((1 - b1 - black) / (1 - black))))
        
        #  Black color
        k = round(black * 100)
        #  Display Calculate results
        print("\n Calculate CMYK", end = "")
        print("\n C =", c, end = "")
        print("\n M =", m, end = "")
        print("\n Y =", y, end = "")
        print("\n K =", k ," ")
    

def main() :
    color = ColorConversion()
    #  Test Case
    color.rgb_to_cmyk(181, 122, 230)
    color.rgb_to_cmyk(78, 37, 78)

if __name__ == "__main__": main()

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
#   Ruby Program
#   RGB to CMYK color

class ColorConversion 
    #  Returns the maximum value of given two numbers
    def max_value(a, b) 
        if (a > b) 
            return a
        else 
            return b
        end

    end

    #  Convert RGB to CMYK
    def rgb_to_cmyk(r, g, b) 
        if (r < 0 || g < 0 || b < 0) 
            return
        end

        print("\n Given RGB")
        print("\n R = ", r)
        print("\n G = ", g)
        print("\n B = ", b)
        #  Calculate R' G' B'
        r1 = r / 255.0
        g1 = g / 255.0
        b1 = b / 255.0
        #  Black key 
        black = (1 - self.max_value(self.max_value(r1, g1), b1))
        c = 0
        m = 0
        y = 0
        k = 0
        if (black != 1) 
            #  Cyan color 
            c = (100 * ((1 - r1 - black) / (1 - black))).round
            #  Magenta color 
            m = (100 * ((1 - g1 - black) / (1 - black))).round
            #  Yellow color
            y = (100 * ((1 - b1 - black) / (1 - black))).round
        end

        #  Black color
        k = (black * 100).round
        #  Display Calculate results
        print("\n Calculate CMYK")
        print("\n C = ", c)
        print("\n M = ", m)
        print("\n Y = ", y)
        print("\n K = ", k ," \n")
    end

end

def main() 
    color = ColorConversion.new()
    #  Test Case
    color.rgb_to_cmyk(181, 122, 230)
    color.rgb_to_cmyk(78, 37, 78)
end

main()

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10 

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69 
/*
  Scala Program
  RGB to CMYK color
*/
class ColorConversion
{
    // Returns the maximum value of given two numbers
    def max_value(a: Double, b: Double): Double = {
        if (a > b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    // Convert RGB to CMYK
    def rgb_to_cmyk(r: Int, g: Int, b: Int): Unit = {
        if (r < 0 || g < 0 || b < 0)
        {
            return;
        }
        print("\n Given RGB");
        print("\n R = " + r);
        print("\n G = " + g);
        print("\n B = " + b);
        // Calculate R' G' B'
        var r1: Double = (r / 255.0);
        var g1: Double = (g / 255.0);
        var b1: Double = (b / 255.0);
        // Black key
        var black: Double = (1 - this.max_value(this.max_value(r1, g1), b1));
        var c: Long = 0;
        var m: Long = 0;
        var y: Long = 0;
        var k: Long = 0;
        if (black != 1)
        {
            // Cyan color
            c = Math.round(100 * (((1 - r1 - black) / (1 - black))));
            // Magenta color
            m = Math.round(100 * (((1 - g1 - black) / (1 - black))));
            // Yellow color
            y = Math.round(100 * (((1 - b1 - black) / (1 - black))));
        }
        // Black color
        k = Math.round(black * 100);
        // Display Calculate results
        print("\n Calculate CMYK");
        print("\n C = " + c);
        print("\n M = " + m);
        print("\n Y = " + y);
        print("\n K = " + k + " \n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var color: ColorConversion = new ColorConversion();
        // Test Case
        color.rgb_to_cmyk(181, 122, 230);
        color.rgb_to_cmyk(78, 37, 78);
    }
}

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69
import Foundation
/*
  Swift 4 Program
  RGB to CMYK color
*/
class ColorConversion
{
    // Returns the maximum value of given two numbers
    func max_value(_ a: Double, _ b: Double)->Double
    {
        if (a > b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    // Convert RGB to CMYK
    func rgb_to_cmyk(_ r: Int, _ g: Int, _ b: Int)
    {
        if (r < 0 || g < 0 || b < 0)
        {
            return;
        }
        print("\n Given RGB", terminator: "");
        print("\n R = ", r, terminator: "");
        print("\n G = ", g, terminator: "");
        print("\n B = ", b, terminator: "");
        // Calculate R" G" B"
        let r1: Double = Double(r) / 255.0;
        let g1: Double = Double(g) / 255.0;
        let b1: Double = Double(b) / 255.0;
        // Black key
        let black: Double = (1 - self.max_value(self.max_value(r1, g1), b1));
        var c: Int = 0;
        var m: Int = 0;
        var y: Int = 0;
        var k: Int = 0;
        if (black != 1)
        {
            // Cyan color
            c = Int(round(100 * ((1 - r1 - black) / (1 - black))));
            // Magenta color
            m = Int(round(100 * ((1 - g1 - black) / (1 - black))));
            // Yellow color
            y = Int(round(100 * ((1 - b1 - black) / (1 - black))));
        }
        // Black color
        k = Int(round(black * 100));
        // Display Calculate results
        print("\n Calculate CMYK", terminator: "");
        print("\n C = ", c, terminator: "");
        print("\n M = ", m, terminator: "");
        print("\n Y = ", y, terminator: "");
        print("\n K = ", k ," ");
    }
}
func main()
{
    let color: ColorConversion = ColorConversion();
    // Test Case
    color.rgb_to_cmyk(181, 122, 230);
    color.rgb_to_cmyk(78, 37, 78);
}
main();

Output

 Given RGB
 R =  181
 G =  122
 B =  230
 Calculate CMYK
 C =  21
 M =  47
 Y =  0
 K =  10

 Given RGB
 R =  78
 G =  37
 B =  78
 Calculate CMYK
 C =  0
 M =  53
 Y =  0
 K =  69
/*
  Kotlin Program
  RGB to CMYK color
*/
class ColorConversion
{
    // Returns the maximum value of given two numbers
    fun max_value(a: Double, b: Double): Double
    {
        if (a>b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
    // Convert RGB to CMYK
    fun rgb_to_cmyk(r: Int, g: Int, b: Int): Unit
    {
        if (r<0 || g<0 || b<0)
        {
            return;
        }
        print("\n Given RGB");
        print("\n R = " + r);
        print("\n G = " + g);
        print("\n B = " + b);
        // Calculate R' G' B'
        var r1: Double = r / 255.0;
        var g1: Double = g / 255.0;
        var b1: Double = b / 255.0;
        // Black key
        var black: Double = (1 - this.max_value(this.max_value(r1, g1), b1));
        var c: Long = 0;
        var m: Long = 0;
        var y: Long = 0;
        var k: Long ;
        if (black != 1.0)
        {
            // Cyan color
            c = Math.round(100 * ((1 - r1 - black) / (1 - black)));
            // Magenta color
            m = Math.round(100 * ((1 - g1 - black) / (1 - black)));
            // Yellow color
            y = Math.round(100 * ((1 - b1 - black) / (1 - black)));
        }
        // Black color
        k = Math.round(black * 100);
        // Display Calculate results
        print("\n Calculate CMYK");
        print("\n C = " + c);
        print("\n M = " + m);
        print("\n Y = " + y);
        print("\n K = " + k + " \n");
    }
}
fun main(args: Array<String>): Unit
{
    var color: ColorConversion = ColorConversion();
    // Test Case
    color.rgb_to_cmyk(181, 122, 230);
    color.rgb_to_cmyk(78, 37, 78);
}

Output

 Given RGB
 R = 181
 G = 122
 B = 230
 Calculate CMYK
 C = 21
 M = 47
 Y = 0
 K = 10

 Given RGB
 R = 78
 G = 37
 B = 78
 Calculate CMYK
 C = 0
 M = 53
 Y = 0
 K = 69

Resultant Output Explanation

For the first test case (181, 122, 230):

  • Normalized RGB values: r1 = 0.7098, g1 = 0.4784, b1 = 0.9020
  • Calculated CMYK values: c = 21, m = 47, y = 0, k = 10

For the second test case (78, 37, 78):

  • Normalized RGB values: r1 = 0.3059, g1 = 0.1451, b1 = 0.3059
  • Calculated CMYK values: c = 0, m = 53, y = 0, k = 69

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.

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