Skip to content

setDrawColor sets incorrect color with CMYK input #2274

@SmythConor

Description

@SmythConor

When using setDrawColor with CMYK input, the color gets set incorrectly.
Reproduced on the live demo using the following code:

var doc = new jsPDF();

doc.setDrawColor(0.0, 0.0, 0.0, 1.0); // Should set draw color to black
doc.rect(20, 20, 10, 10);

The resulting output on the PDF displays a square with white borders.

The issue revolves around the encodeColorString function. When the "assume CMYK" branch is reached, the color values passed in are divided by 255 so they're being treated like RGB values. The documentation specifies that if using CMYK, the values passed in should be between 0.0 (0%) to 1.0 (100%) so there is no need to divide the parameters by anything to get the correct CMYK output.

Exact code snippet:

// assume CMYK
if (typeof ch1 === 'string') {
	color = [ch1, ch2, ch3, ch4, letterArray[2]].join(" ");
} else {
	switch (options.precision) {
		case 2:
                        // ** These should not be divided by 255 **
			color = [f2(ch1 / 255), f2(ch2 / 255), f2(ch3 / 255), f2(ch4 / 255), letterArray[2]].join(" ");
			break;

		case 3:
		default:
                        // ** These should not be divided by 255 **
			color = [f3(ch1 / 255), f3(ch2 / 255), f3(ch3 / 255), f3(ch4 / 255), letterArray[2]].join(" ");
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions