RGB to CMYK color

Here given code implementation process.

// 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


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







© 2021, kalkicode.com, All rights reserved