|
1 | 1 | import { |
2 | 2 | ROLES, MAXIMUM, FIXED, ALIAS, |
3 | | - RUNNING, PLAY, ENDED, PLAY_CSS, CURRENT_TIME, START_ANIMATION, EASINGS, NAME_SEPARATOR |
| 3 | + RUNNING, PLAY, ENDED, PLAY_CSS, CURRENT_TIME, |
| 4 | + START_ANIMATION, EASINGS, NAME_SEPARATOR |
4 | 5 | } from "./consts"; |
5 | 6 | import PropertyObject from "./PropertyObject"; |
6 | 7 | import Scene from "./Scene"; |
7 | 8 | import SceneItem from "./SceneItem"; |
8 | 9 | import { |
9 | 10 | isArray, ANIMATION, ARRAY, OBJECT, |
10 | | - PROPERTY, STRING, NUMBER, IS_WINDOW, IObject, $, isObject, addEvent, removeEvent, isString, |
| 11 | + PROPERTY, STRING, NUMBER, IS_WINDOW, IObject, |
| 12 | + $, isObject, addEvent, removeEvent, isString, |
| 13 | + splitComma, splitBracket, |
11 | 14 | } from "@daybrush/utils"; |
12 | 15 | import { EasingType, EasingFunction, NameType, SelectorAllType } from "./types"; |
13 | 16 | import { toPropertyObject } from "./utils/property"; |
@@ -294,3 +297,38 @@ export function selectorAll(callback: (index: number) => any, defaultCount = 0): |
294 | 297 |
|
295 | 298 | return nextCallback; |
296 | 299 | } |
| 300 | + |
| 301 | +export function rgbaToHexa(rgba: string) { |
| 302 | + const hexInfo = rgbaToHexWithOpacity(rgba); |
| 303 | + const hex = hexInfo.hex; |
| 304 | + |
| 305 | + if (!hexInfo.hex) { |
| 306 | + return ""; |
| 307 | + } |
| 308 | + const opacityHex = Math.floor(hexInfo.opacity * 255).toString(16); |
| 309 | + |
| 310 | + return `${hex}${opacityHex}`; |
| 311 | +} |
| 312 | + |
| 313 | +export function rgbaToHexWithOpacity(rgba: string) { |
| 314 | + const rgbaInfo = splitBracket(rgba); |
| 315 | + |
| 316 | + if (rgbaInfo.prefix.indexOf("rgb") !== 0) { |
| 317 | + return { |
| 318 | + hex: "", |
| 319 | + opacity: 1, |
| 320 | + }; |
| 321 | + } |
| 322 | + |
| 323 | + const rgbaArr = splitComma(rgbaInfo.value); |
| 324 | + const rgbaNums = rgbaArr.slice(0, 3).map(num => { |
| 325 | + const dec = parseInt(num, 10); |
| 326 | + |
| 327 | + return dec.toString(16); |
| 328 | + }); |
| 329 | + |
| 330 | + return { |
| 331 | + hex: `#${rgbaNums.join("")}`, |
| 332 | + opacity: rgbaArr[3] ? parseFloat(rgbaArr[3]) : 1, |
| 333 | + }; |
| 334 | +} |
0 commit comments