From b5fc96a3637756e1c432464c18907f010311766e Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Sat, 1 Dec 2018 14:53:05 +0100 Subject: clamp values in toHex, tests, replace for of with for in --- src/types/Color.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src') 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 } -- cgit v1.2.3