|
1 | | -import normalizeColor from 'normalize-css-color' |
| 1 | +import normalizeColor from '../normalize-css-color/index.js' |
2 | 2 |
|
3 | 3 | var linear = t => t |
4 | 4 |
|
@@ -118,17 +118,30 @@ function colorToRgba(input) { |
118 | 118 |
|
119 | 119 | var stringShapeRegex = /[0-9\.-]+/g |
120 | 120 |
|
| 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 | + |
121 | 130 | /** |
122 | 131 | * Supports string shapes by extracting numbers so new values can be computed, |
123 | 132 | * and recombines those values into new strings of the same shape. Supports |
124 | 133 | * things like: |
125 | 134 | * |
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 |
128 | 138 | */ |
129 | 139 | function createInterpolationFromStringOutputRange(config) { |
130 | 140 | 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)) |
132 | 145 |
|
133 | 146 | // -> |
134 | 147 | // [ |
@@ -160,17 +173,24 @@ function createInterpolationFromStringOutputRange(config) { |
160 | 173 | return Interpolation.create({ ...config, output: outputRanges[i] }) |
161 | 174 | }) |
162 | 175 |
|
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]) |
166 | 176 | 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 | + ) |
174 | 194 | } |
175 | 195 | } |
176 | 196 |
|
|
0 commit comments