Skip to content

fix(gray.cmyk): complement lightness to get the K channel - #133

Open
spokodev wants to merge 1 commit into
Qix-:masterfrom
spokodev:fix/gray-cmyk-invert
Open

spokodev wants to merge 1 commit into
Qix-:masterfrom
spokodev:fix/gray-cmyk-invert

Conversation

@spokodev

Copy link
Copy Markdown

What

Everywhere in color-convert, gray is a lightness percentage where 100 is white and 0 is black — gray.rgb([100]) === [255, 255, 255], gray.hex([100]) === 'FFFFFF', gray.lab([100]) === [100, 0, 0], and the inverse rgb.gray([255, 255, 255]) === [100].

gray.cmyk put that lightness straight into the CMYK K (black/darkness) channel, which has the opposite polarity. So it inverted every value except the symmetric midpoint 50:

convert.gray.cmyk([100]) // [0, 0, 0, 100] -> K=100 = black, but gray 100 is white
convert.gray.cmyk([0])   // [0, 0, 0, 0]   -> K=0   = white, but gray 0 is black

The library's own gray -> rgb -> cmyk path gives the correct [0, 0, 0, 0] for white and [0, 0, 0, 100] for black, confirming the direct conversion is wrong.

Fix

K is the complement of lightness, so return 100 - gray[0]. This now matches every sibling gray.* conversion and the routed gray -> rgb -> cmyk result for all inputs.

Tests

The only existing gray.cmyk assertion used input 50, the one value where the inversion is invisible (100 - 50 === 50). Added assertions for 100 and 0; both fail on master and pass with the fix. xo and node test/basic.js are green.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant