summaryrefslogtreecommitdiffstats
path: root/src/types
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-12-01 14:53:05 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-12-01 14:53:05 +0100
commitb5fc96a3637756e1c432464c18907f010311766e (patch)
tree51f95062ca080f5ebf50138308f1f1326f4e0d51 /src/types
parentf4531868a190af69c4ecdcf6d7be6d3fc59f5d46 (diff)
downloadsvg.js-b5fc96a3637756e1c432464c18907f010311766e.tar.gz
svg.js-b5fc96a3637756e1c432464c18907f010311766e.zip
clamp values in toHex, tests, replace for of with for in
Diffstat (limited to 'src/types')
-rw-r--r--src/types/Color.js19
1 files changed, 11 insertions, 8 deletions
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
}