]> source.dussan.org Git - svg.js.git/commitdiff
clamp values in toHex, tests, replace for of with for in
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>
Sat, 1 Dec 2018 13:53:05 +0000 (14:53 +0100)
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>
Sat, 1 Dec 2018 13:53:05 +0000 (14:53 +0100)
spec/spec/color.js
src/types/Color.js

index 973e323734b12913452c9f23029b0f0a4170f528..1f0dfbd802e9a76a49fb125552edfa089511c7eb 100644 (file)
@@ -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')
                        })
 
                })
index b745bfd5a77f3f34292dd1b32b41e4d0ea82ab7d..ea9f6743be6da1f1c433f1e9760c911406e10d12 100644 (file)
@@ -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
   }