Skip to content

Commit 41aaafb

Browse files
committed
https://github.com/animatedjs/animated/pull/47
1 parent 455886a commit 41aaafb

File tree

5 files changed

+400
-16
lines changed

5 files changed

+400
-16
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
},
7777
"dependencies": {
7878
"@babel/runtime": "^7.0.0-beta.44",
79-
"create-react-context": "^0.2.2",
80-
"normalize-css-color": "^1.0.2"
79+
"create-react-context": "^0.2.2"
8180
}
8281
}

src/animated/Interpolation.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import normalizeColor from 'normalize-css-color'
1+
import normalizeColor from '../normalize-css-color/index.js'
22

33
var linear = t => t
44

@@ -118,17 +118,30 @@ function colorToRgba(input) {
118118

119119
var stringShapeRegex = /[0-9\.-]+/g
120120

121+
// Covers rgb, rgba, hsl, hsla
122+
// Taken from https://gist.github.com/olmokramer/82ccce673f86db7cda5e
123+
var colorRegex = /(#[\d\w]+|\w+\((?:\d+%?(?:,\s)*){3}(?:\d*\.?\d+)?\))/
124+
// Covers color names (transparent, blue, etc.)
125+
var colorNamesRegex = new RegExp(
126+
`(${Object.keys(normalizeColor.colorNames).join('|')})`,
127+
'g'
128+
)
129+
121130
/**
122131
* Supports string shapes by extracting numbers so new values can be computed,
123132
* and recombines those values into new strings of the same shape. Supports
124133
* things like:
125134
*
126-
* rgba(123, 42, 99, 0.36) // colors
127-
* -45deg // values with units
135+
* rgba(123, 42, 99, 0.36) // colors
136+
* 0 2px 2px 0px rgba(0, 0, 0, 0.12) // box shadows
137+
* -45deg // values with units
128138
*/
129139
function createInterpolationFromStringOutputRange(config) {
130140
var outputRange = config.output
131-
outputRange = outputRange.map(colorToRgba)
141+
// Replace colors with rgba
142+
outputRange = outputRange
143+
.map(rangeValue => rangeValue.replace(colorRegex, colorToRgba))
144+
.map(rangeValue => rangeValue.replace(colorNamesRegex, colorToRgba))
132145

133146
// ->
134147
// [
@@ -160,17 +173,24 @@ function createInterpolationFromStringOutputRange(config) {
160173
return Interpolation.create({ ...config, output: outputRanges[i] })
161174
})
162175

163-
// rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to
164-
// round the opacity (4th column).
165-
const shouldRound = /^rgb/.test(outputRange[0])
166176
return input => {
167-
var i = 0 // 'rgba(0, 100, 200, 0)'
168-
// ->
169-
// 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...'
170-
return outputRange[0].replace(stringShapeRegex, () => {
171-
const val = interpolations[i++](input)
172-
return String(shouldRound && i < 4 ? Math.round(val) : val)
173-
})
177+
var i = 0
178+
return (
179+
outputRange[0]
180+
// 'rgba(0, 100, 200, 0)'
181+
// ->
182+
// 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...'
183+
.replace(stringShapeRegex, () => interpolations[i++](input))
184+
// rgba requires that the r,g,b are integers.... so we want to round them, but we *dont* want to
185+
// round the opacity (4th column).
186+
.replace(
187+
/rgba\(([0-9\.-]+), ([0-9\.-]+), ([0-9\.-]+), ([0-9\.-]+)\)/gi,
188+
(_, p1, p2, p3, p4) =>
189+
`rgba(${Math.round(p1)}, ${Math.round(p2)}, ${Math.round(
190+
p3
191+
)}, ${p4})`
192+
)
193+
)
174194
}
175195
}
176196

src/normalize-css-color/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2016, React Community
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/normalize-css-color/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# normalize-css-color
2+
3+
Normalize a subset of CSS color values into integers

0 commit comments

Comments
 (0)