To simulate an over-print rather than a blend using a color-mix function, you will need to focus on how colors combine in an additive or subtractive color model rather than just averaging them.
Over-printing is typically associated with the subtractive color model (CMYK) used in printing. In subtractive mixing, colors are combined by subtracting wavelengths from white light, which differs from additive mixing (RGB) where light is added to create colors.
Here are steps you can take to simulate over-printing:
Approach 1: Subtractive Color Model (CMYK)
-
Convert the input colors from RGB to CMYK: Since CSS and most web color tools work with RGB, you'll need to convert RGB colors to CMYK.
Conversion example from RGB to CMYK:
function rgbToCmyk(r, g, b) {
let c = 1 - (r / 255);
let m = 1 - (g / 255);
let y = 1 - (b / 255);
let minCMY = Math.min(c, Math.min(m, y));
c = (c - minCMY) / (1 - minCMY);
m = (m - minCMY) / (1 - minCMY);
y = (y - minCMY) / (1 - minCMY);
let k = minCMY;
return [c, m, y, k];
}
-
Mix the CMYK values: This is the key step where you simulate over-printing.
- Multiply the respective C, M, Y, and K values of both colors instead of averaging them. Over-printing essentially combines the inks, leading to a darker result.
Mixing example:
function mixCmyk(cmyk1, cmyk2) {
let c = cmyk1[0] * cmyk2[0];
let m = cmyk1[1] * cmyk2[1];
let y = cmyk1[2] * cmyk2[2];
let k = cmyk1[3] * cmyk2[3];
return [c, m, y, k];
}
-
Convert the resulting CMYK color back to RGB:
function cmykToRgb(c, m, y, k) {
let r = 255 * (1 - c) * (1 - k);
let g = 255 * (1 - m) * (1 - k);
let b = 255 * (1 - y) * (1 - k);
return [Math.round(r), Math.round(g), Math.round(b)];
}
Approach 2: Using CSS with Blending Modes (Less Accurate)
In CSS, you can use blending modes to simulate over-print effects to an extent:
- Use the
mix-blend-mode property in CSS:
.overprint {
mix-blend-mode: multiply; /* This is the most similar to overprinting */
}
- The
multiply blend mode will darken the composition by multiplying the colors, which somewhat simulates the effect of ink over-printing.
Here's an example combining both:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Over-print Simulation</title>
<style>
.overprint {
width: 200px;
height: 200px;
mix-blend-mode: multiply; /* Simulates overprint */
}
.color-1 {
background: #ff0000; /* red */
}
.color-2 {
background: #0000ff; /* blue */
}
</style>
</head>
<body>
<div class="color-1 overprint">Color 1</div>
<div class="color-2 overprint">Color 2 on Top</div>
</body>
</html>
These methods provide a way to simulate over-print by handling colors in a manner similar to subtractive mixing models or by leveraging CSS blending modes. However, achieving a perfect simulation requires more complex computations and handling of the subtractive color model.