diff --git a/lib/color.js b/lib/color.js index c8588c7..7ab92ec 100644 --- a/lib/color.js +++ b/lib/color.js @@ -139,7 +139,7 @@ class RGBColor extends ColorValue { #rgb; /** - * @param {string} fn + * @param {string|undefined} fn * @param {[number,number,number]} rgb */ constructor(fn, rgb) { @@ -246,6 +246,22 @@ class RGBPctColor extends ColorValue { * @returns {ColorValue} */ getMinifiedValue() { + /** @type {number[]} */ + const rgb = []; + // If all percentages are 0 or 100, convert to regular rgb. + for (const pct of this.#rgb) { + if (pct === 0) { + rgb.push(0); + } else if (pct === 100) { + rgb.push(255); + } else { + break; + } + } + if (rgb.length === 3) { + // @ts-ignore + return new RGBColor(undefined, rgb).getMinifiedValue(); + } return new ColorValue(`rgb(${this.#rgb.map((n) => n + '%').join(',')})`); } } diff --git a/test/lib/colors.test.js b/test/lib/colors.test.js index 144b0a4..10419e1 100644 --- a/test/lib/colors.test.js +++ b/test/lib/colors.test.js @@ -14,6 +14,8 @@ describe('test parsing and minifying', () => { { in: 'rgb( 49.5%, 33.49% ,22.5% )', minified: 'rgb(49.5%,33.49%,22.5%)' }, { in: 'rgb(165,42,42)', minified: 'brown' }, { in: 'rgb( 50 100 150 /.1)', minified: 'rgb( 50 100 150 /.1)' }, + { in: 'rgb(100%,0%,0%)', minified: 'red' }, + { in: 'rgb(100%,100%,100%)', minified: '#fff' }, ]; for (const testCase of testCases) { it(`${testCase.in}`, () => {