diff --git a/CHANGELOG.md b/CHANGELOG.md index eadd1cd2..57edcd77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Unreleased +- Add support for rgb values in a spot color + ### [v0.17.2] - 2025-08-30 - Fix rendering lists that spans across pages diff --git a/lib/spotcolor.js b/lib/spotcolor.js index 1bbdd054..93aa8679 100644 --- a/lib/spotcolor.js +++ b/lib/spotcolor.js @@ -1,22 +1,42 @@ export default class SpotColor { constructor(doc, name, C, M, Y, K) { - this.id = 'CS' + Object.keys(doc.spotColors).length; - this.name = name; - this.values = [C, M, Y, K]; - this.ref = doc.ref([ - 'Separation', - this.name, - 'DeviceCMYK', - { - Range: [0, 1, 0, 1, 0, 1, 0, 1], - C0: [0, 0, 0, 0], - C1: this.values.map((value) => value / 100), - FunctionType: 2, - Domain: [0, 1], - N: 1, - }, - ]); - this.ref.end(); + if (typeof K === 'undefined') { + this.id = 'CS' + Object.keys(doc.spotColors).length; + this.name = name; + this.values = [C, M, Y]; + this.ref = doc.ref([ + 'Separation', + this.name, + 'DeviceRGB', + { + Range: [0, 1, 0, 1, 0, 1], + C0: [0, 0, 0], + C1: this.values.map((value) => value / 255), + FunctionType: 2, + Domain: [0, 1], + N: 1, + }, + ]); + this.ref.end(); + } else { + this.id = 'CS' + Object.keys(doc.spotColors).length; + this.name = name; + this.values = [C, M, Y, K]; + this.ref = doc.ref([ + 'Separation', + this.name, + 'DeviceCMYK', + { + Range: [0, 1, 0, 1, 0, 1, 0, 1], + C0: [0, 0, 0, 0], + C1: this.values.map((value) => value / 100), + FunctionType: 2, + Domain: [0, 1], + N: 1, + }, + ]); + this.ref.end(); + } } toString() { diff --git a/tests/unit/color.spec.js b/tests/unit/color.spec.js index 0b482205..2fbc021a 100644 --- a/tests/unit/color.spec.js +++ b/tests/unit/color.spec.js @@ -56,4 +56,25 @@ describe('color', function () { '>>', ]); }); + + test('spot color with rgb', function () { + const doc = new PDFDocument(); + const data = logData(doc); + doc.addSpotColor('MAGENTA', 255, 0, 255); + doc.fillColor('MAGENTA').text('This text uses spot color!'); + doc.end(); + + expect(data).toContainChunk([ + `6 0 obj`, + '<<\n' + + '/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]\n' + + '/ColorSpace <<\n' + + '/CS0 8 0 R\n' + + '>>\n' + + '/Font <<\n' + + '/F1 9 0 R\n' + + '>>\n' + + '>>', + ]); + }); });