From: Ulrich-Matthias Schäfer Date: Sat, 1 Dec 2018 13:53:05 +0000 (+0100) Subject: clamp values in toHex, tests, replace for of with for in X-Git-Tag: 3.0.0~10^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b5fc96a3637756e1c432464c18907f010311766e;p=svg.js.git clamp values in toHex, tests, replace for of with for in --- diff --git a/spec/spec/color.js b/spec/spec/color.js index 973e323..1f0dfbd 100644 --- a/spec/spec/color.js +++ b/spec/spec/color.js @@ -116,17 +116,19 @@ describe('Color', function() { }) it('handles black', () => { - let {r, g, b} = new SVG.Color(0, 0, 0).lab().rgb() - expect( r ).toBeCloseTo(0, 0) - expect( g ).toBeCloseTo(0, 0) - expect( b ).toBeCloseTo(0, 0) + let color = new SVG.Color(0, 0, 0).lab().rgb() + expect( color.r ).toBeCloseTo(0, 0) + expect( color.g ).toBeCloseTo(0, 0) + expect( color.b ).toBeCloseTo(0, 0) + expect( color.toHex() ).toBe('#000000') }) it('handles white', () => { - let {r, g, b} = new SVG.Color(255, 255, 255).lab().rgb() - expect( r ).toBeCloseTo(255, 0) - expect( g ).toBeCloseTo(255, 0) - expect( b ).toBeCloseTo(255, 0) + let color = new SVG.Color(255, 255, 255).lab().rgb() + expect( color.r ).toBeCloseTo(255, 0) + expect( color.g ).toBeCloseTo(255, 0) + expect( color.b ).toBeCloseTo(255, 0) + expect( color.toHex() ).toBe('#ffffff') }) }) @@ -157,17 +159,19 @@ describe('Color', function() { }) it('handles black', () => { - let {r, g, b} = new SVG.Color(0, 0, 0).lch().rgb() - expect( r ).toBeCloseTo(0, 0) - expect( g ).toBeCloseTo(0, 0) - expect( b ).toBeCloseTo(0, 0) + let color = new SVG.Color(0, 0, 0).lch().rgb() + expect( color.r ).toBeCloseTo(0, 0) + expect( color.g ).toBeCloseTo(0, 0) + expect( color.b ).toBeCloseTo(0, 0) + expect( color.toHex() ).toBe('#000000') }) it('handles white', () => { - let {r, g, b} = new SVG.Color(255, 255, 255).lch().rgb() - expect( r ).toBeCloseTo(255, 0) - expect( g ).toBeCloseTo(255, 0) - expect( b ).toBeCloseTo(255, 0) + let color = new SVG.Color(255, 255, 255).lch().rgb() + expect( color.r ).toBeCloseTo(255, 0) + expect( color.g ).toBeCloseTo(255, 0) + expect( color.b ).toBeCloseTo(255, 0) + expect( color.toHex() ).toBe('#ffffff') }) }) @@ -199,17 +203,19 @@ describe('Color', function() { }) it('handles black', () => { - let {r, g, b} = new SVG.Color(0, 0, 0).hsl().rgb() - expect( r ).toBeCloseTo(0, 0) - expect( g ).toBeCloseTo(0, 0) - expect( b ).toBeCloseTo(0, 0) + let color = new SVG.Color(0, 0, 0).hsl().rgb() + expect( color.r ).toBeCloseTo(0, 0) + expect( color.g ).toBeCloseTo(0, 0) + expect( color.b ).toBeCloseTo(0, 0) + expect( color.toHex() ).toBe('#000000') }) it('handles white', () => { - let {r, g, b} = new SVG.Color(255, 255, 255).hsl().rgb() - expect( r ).toBeCloseTo(255, 0) - expect( g ).toBeCloseTo(255, 0) - expect( b ).toBeCloseTo(255, 0) + let color = new SVG.Color(255, 255, 255).hsl().rgb() + expect( color.r ).toBeCloseTo(255, 0) + expect( color.g ).toBeCloseTo(255, 0) + expect( color.b ).toBeCloseTo(255, 0) + expect( color.toHex() ).toBe('#ffffff') }) }) @@ -242,17 +248,19 @@ describe('Color', function() { }) it('handles black', () => { - let {r, g, b} = new SVG.Color(0, 0, 0).cmyk().rgb() - expect( r ).toBeCloseTo(0, 0) - expect( g ).toBeCloseTo(0, 0) - expect( b ).toBeCloseTo(0, 0) + let color = new SVG.Color(0, 0, 0).cmyk().rgb() + expect( color.r ).toBeCloseTo(0, 0) + expect( color.g ).toBeCloseTo(0, 0) + expect( color.b ).toBeCloseTo(0, 0) + expect( color.toHex() ).toBe('#000000') }) it('handles white', () => { - let {r, g, b} = new SVG.Color(255, 255, 255).cmyk().rgb() - expect( r ).toBeCloseTo(255, 0) - expect( g ).toBeCloseTo(255, 0) - expect( b ).toBeCloseTo(255, 0) + let color = new SVG.Color(255, 255, 255).cmyk().rgb() + expect( color.r ).toBeCloseTo(255, 0) + expect( color.g ).toBeCloseTo(255, 0) + expect( color.b ).toBeCloseTo(255, 0) + expect( color.toHex() ).toBe('#ffffff') }) }) diff --git a/src/types/Color.js b/src/types/Color.js index b745bfd..ea9f674 100644 --- a/src/types/Color.js +++ b/src/types/Color.js @@ -18,8 +18,8 @@ function componentHex (component) { } function is (object, space) { - for (const key of space) { - if (object[key] == null) { + for (let i = space.length; i--;) { + if (object[space[i]] == null) { return false } } @@ -318,9 +318,15 @@ export default class Color { Input and Output methods */ - toHex () { + _clamped () { let { _a, _b, _c } = this.rgb() - let [ r, g, b ] = [ _a, _b, _c ].map(componentHex) + let { max, min, round } = Math + let format = v => max(0, min(round(v), 255)) + return [ _a, _b, _c ].map(format) + } + + toHex () { + let [ r, g, b ] = this._clamped().map(componentHex) return `#${r}${g}${b}` } @@ -329,10 +335,7 @@ export default class Color { } toRgb () { - let { r, g, b } = this.rgb() - let { max, min, round } = Math - let format = v => max(0, min(round(v), 255)) - let [ rV, gV, bV ] = [ r, g, b ].map(format) + let [ rV, gV, bV ] = this._clamped() let string = `rgb(${rV},${gV},${bV})` return string }