| 233 | const C_PERCENT = '%'.charCodeAt(0); |
| 234 | |
| 235 | function getNumbersFromString(input: string, range: number[], units: {[unit: string]: number}) { |
| 236 | const numbers: number[] = []; |
| 237 | const searchStart = input.indexOf('(') + 1; |
| 238 | const searchEnd = input.length - 1; |
| 239 | let numStart = -1; |
| 240 | let unitStart = -1; |
| 241 | |
| 242 | const push = (matchEnd: number) => { |
| 243 | const numEnd = unitStart > -1 ? unitStart : matchEnd; |
| 244 | const $num = input.slice(numStart, numEnd); |
| 245 | let n = parseFloat($num); |
| 246 | const r = range[numbers.length]; |
| 247 | if (unitStart > -1) { |
| 248 | const unit = input.slice(unitStart, matchEnd); |
| 249 | const u = units[unit]; |
| 250 | if (u != null) { |
| 251 | n *= r / u; |
| 252 | } |
| 253 | } |
| 254 | if (r > 1) { |
| 255 | n = Math.round(n); |
| 256 | } |
| 257 | numbers.push(n); |
| 258 | numStart = -1; |
| 259 | unitStart = -1; |
| 260 | }; |
| 261 | |
| 262 | for (let i = searchStart; i < searchEnd; i++) { |
| 263 | const c = input.charCodeAt(i); |
| 264 | const isNumChar = (c >= C_0 && c <= C_9) || c === C_DOT || c === C_PLUS || c === C_MINUS || c === C_e; |
| 265 | const isDelimiter = c === C_SPACE || c === C_COMMA || c === C_SLASH; |
| 266 | if (isNumChar) { |
| 267 | if (numStart === -1) { |
| 268 | numStart = i; |
| 269 | } |
| 270 | } else if (numStart > -1) { |
| 271 | if (isDelimiter) { |
| 272 | push(i); |
| 273 | } else if (unitStart === -1) { |
| 274 | unitStart = i; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | if (numStart > -1) { |
| 279 | push(searchEnd); |
| 280 | } |
| 281 | return numbers; |
| 282 | } |
| 283 | |
| 284 | const rgbRange = [255, 255, 255, 1]; |
| 285 | const rgbUnits = {'%': 100}; |