From c6d9145de7dee780a3ea96408cfca0d795ebc92e Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 23 Jul 2026 13:23:22 +0100 Subject: [PATCH] fix(gray.cmyk): complement lightness to get the K channel Everywhere in the library `gray` is a lightness percentage where 100 is white and 0 is black (gray.rgb, gray.hsl, gray.hsv, gray.lab, gray.hex, and the inverse rgb.gray). `gray.cmyk` instead put the lightness straight into the K (black) channel, which has the opposite polarity, so it inverted every value except the symmetric midpoint 50: convert.gray.cmyk([100]) // [0, 0, 0, 100] -> black, but gray 100 is white convert.gray.cmyk([0]) // [0, 0, 0, 0] -> white, but gray 0 is black K is the complement of lightness, so return `100 - gray[0]`. This matches the library's own gray -> rgb -> cmyk path for every input. The existing test only covered gray 50 (the fixed point where the bug is invisible); added assertions for 100 and 0. --- conversions.js | 4 +++- test/basic.js | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/conversions.js b/conversions.js index c6aac2c..721318e 100644 --- a/conversions.js +++ b/conversions.js @@ -957,7 +957,9 @@ convert.gray.hwb = function (gray) { }; convert.gray.cmyk = function (gray) { - return [0, 0, 0, gray[0]]; + // Gray is a lightness percentage (100 = white); CMYK K is darkness, + // so K is its complement, matching every other gray.* conversion + return [0, 0, 0, 100 - gray[0]]; }; convert.gray.lab = function (gray) { diff --git a/test/basic.js b/test/basic.js index 3e413a7..f3ec2f7 100644 --- a/test/basic.js +++ b/test/basic.js @@ -238,6 +238,8 @@ assert.deepStrictEqual(convert.gray.hsl([50]), [0, 0, 50]); assert.deepStrictEqual(convert.gray.hsv([50]), [0, 0, 50]); assert.deepStrictEqual(convert.gray.hwb([50]), [0, 100, 50]); assert.deepStrictEqual(convert.gray.cmyk([50]), [0, 0, 0, 50]); +assert.deepStrictEqual(convert.gray.cmyk([100]), [0, 0, 0, 0]); +assert.deepStrictEqual(convert.gray.cmyk([0]), [0, 0, 0, 100]); assert.deepStrictEqual(convert.gray.lab([50]), [50, 0, 0]); assert.deepStrictEqual(convert.gray.hex([50]), '808080'); assert.deepStrictEqual(convert.gray.hex([100]), 'FFFFFF');