CMY and CMYK Color Models using Python


The CMY and CMYK color models are subtractive color models used in printing and graphic design. In Python, we can work with these color models using various libraries and tools. Let's see each color model in detail.

CMY color model

The CMY color model, also known as the subtractive color model, which is a system used for mixing colors in various applications like printing, painting, and graphic design. CMY stands for Cyan, Magenta, and Yellow, which are the primary colors in this model.

In the CMY color model, colors are created by subtracting different amounts of cyan, magenta, and yellow pigments from white light. The more of each pigment is present, the more it subtracts light, which results in the perception of different colors. The following is the detailed explanation of each primary color in the CMY model.

  • Cyan (C) − Cyan is a bluish-green color. It absorbs red light, by allowing only green and blue light to be reflected, which gives it its characteristic color.

  • Magenta (M) − Magenta is a purplish-red color. It absorbs green light, by allowing only red and blue light to be reflected, which gives it its characteristic color.

  • Yellow (Y) − Yellow is a bright color. It absorbs blue light, allowing only red and green light to be reflected, which gives it its characteristic color.

The CMY model

For creating different colors using the CMY model, the three primary colors are combined in varying amounts. The below points define how the color mixing works in CMY color model.

  • Combining cyan and magenta pigments absorbs both red and green light, resulting in the perception of blue light. This mixture creates shades of blue.

  • Combining cyan and yellow pigments absorbs both red and blue light, resulting in the perception of green light. This mixture creates shades of green.

  • Combining magenta and yellow pigments absorbs both green and blue light, resulting in the perception of red light. This mixture creates shades of red.

  • Combining all three primary colors cyan, magenta, and yellow in equal amounts absorbs all three primary lights red, green, and blue, resulting in the absence of reflected light, which appears as black.

In the CMY color model, the absence of all three primary colors Cyan, Magenta, and Yellow represents white. This is opposite to the additive color model RGB, where the absence of all three primary colors Red, Green, and Blue represents black.

To work with the CMY color model in Python, we can use libraries such as PIL (Python Imaging Library) or matplotlib. These libraries provide functionalities to create images, set pixel values, retrieve color information, and perform various operations using the CMY color model.

Example

In this example, we define the CMY color using a tuple (0.5, 0.2, 0.5) where each value represents the Cyan, Magenta, and Yellow components, respectively. We are creating a figure using plt.subplots() and add a rectangle patch with the given CMY color fc=cmy_color. The ax.set_aspect('equal') line ensures that the aspect ratio of the plot is maintained to display a square shape.

import matplotlib.pyplot as plt
# CMY color model
cmy_color = (0.5, 0.2, 0.5)  # CMY values range from 0 to 1
# Create a figure with a colored rectangle
fig, ax = plt.subplots()
ax.add_patch(plt.Rectangle((0, 0), 1, 1, fc=cmy_color, ec='black'))
ax.set_aspect('equal')  # Set aspect ratio to maintain square shape
# Display the figure
plt.show()

Output

CMYK Color Model

The CMYK color model is also a subtractive color model which is used in printing and graphic design. CMYK stands for Cyan, Magenta, Yellow, and Key (black). CMYK model works by subtracting different amounts of ink or pigment from a white background to produce colors.

The CMYK color model uses a percentage system, where each component is represented by a value ranging from 0 to 100. The value of 0 indicates no ink or pigment, while a value of 100 represents the maximum amount of ink or pigment for that component. For example, pure cyan would be represented as (100, 0, 0, 0), indicating 100% cyan and 0% magenta, yellow, and black. The following is the detailed explanation of each primary color in the CMYK model.

  • Cyan − Cyan represents the intensity of cyan ink. It absorbs red light and reflects green and blue light. High levels of cyan result in more intense blues and greens.

  • Magenta − Magenta represents the intensity of magenta ink. It absorbs green light and reflects red and blue light. Higher levels of magenta produce stronger reds and purples.

  • Yellow − Yellow represents the intensity of yellow ink. It absorbs blue light and reflects red and green light. Higher levels of yellow create vibrant yellows and oranges.

  • Key (Black) − Key represents the intensity of black ink. The black component is added to the CMY inks for better contrast, improved shadow detail, and more accurate reproduction of dark colors.

Example

In this example, mcolors.to_rgb() is used to convert the CMYK values (0.3, 0.6, 0.5, 0.8) to an RGB equivalent that can be used with matplotlib library. Next, a figure and axes are created using plt.subplots() function available in matplotlib.

A rectangle patch is created using plt.Rectangle() and its facecolor parameter is set to the CMYK color. The rectangle is added to the axes using ax.add_patch() and the axis limits are set to (0, 1) to ensure the entire rectangle is visible. The aspect ratio is set to 'equal' to maintain a square shape for the plot. Finally, the plot is displayed using plt.show().

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
# Create a CMYK color
cmyk_color = mcolors.to_rgb((0.3, 0.6, 0.5, 0.8))  # CMYK values range from 0 to 1
# Create a figure and axes
fig, ax = plt.subplots()
# Draw a rectangle filled with the CMYK color
rectangle = plt.Rectangle((0.2, 0.2), 0.6, 0.6, facecolor=cmyk_color)
ax.add_patch(rectangle)
# Set the axis limits
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# Set the aspect ratio
ax.set_aspect('equal')
# Show the plot
plt.show()

Output

Updated on: 02-Aug-2023

604 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements