Skip to content

Commit cb71d62

Browse files
committed
feat: add rgbaToHexa
1 parent 54f971a commit cb71d62

File tree

7 files changed

+89
-4
lines changed

7 files changed

+89
-4
lines changed

packages/scenejs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scenejs",
3-
"version": "1.8.5",
3+
"version": "1.9.0",
44
"description": "JavaScript & CSS timeline-based animation library",
55
"main": "dist/scene.cjs.js",
66
"module": "dist/scene.esm.js",
@@ -48,6 +48,7 @@
4848
"@types/chai": "^4.1.7",
4949
"@types/karma-chai": "^0.1.1",
5050
"@types/mocha": "^5.2.6",
51+
"@types/node": "^18.11.17",
5152
"@types/sinon": "^10.0.11",
5253
"chai": "^4.2.0",
5354
"coveralls": "^3.0.0",

packages/scenejs/src/index.cjs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Scene, * as others from "./index";
33
for (const name in others) {
44
(Scene as any)[name] = (others as any)[name];
55
}
6+
declare const module: any;
67

78
export * from "./index";
89
module.exports = Scene;

packages/scenejs/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export * from "./easing";
88
export * from "./presets";
99
export * from "./types";
1010
export { OPTIONS, EVENTS, FIXED, ROLES, NAME_SEPARATOR } from "./consts";
11-
export { setRole, setAlias, isRole, isScene, isSceneItem, isFrame, selectorAll } from "./utils";
11+
export {
12+
setRole, setAlias, isRole,
13+
isScene, isSceneItem,
14+
isFrame, selectorAll,
15+
rgbaToHexWithOpacity,
16+
rgbaToHexa,
17+
} from "./utils";
1218
export { Scene as default };
1319
export * from "./reactive";

packages/scenejs/src/utils.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import {
22
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
45
} from "./consts";
56
import PropertyObject from "./PropertyObject";
67
import Scene from "./Scene";
78
import SceneItem from "./SceneItem";
89
import {
910
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,
1114
} from "@daybrush/utils";
1215
import { EasingType, EasingFunction, NameType, SelectorAllType } from "./types";
1316
import { toPropertyObject } from "./utils/property";
@@ -294,3 +297,38 @@ export function selectorAll(callback: (index: number) => any, defaultCount = 0):
294297

295298
return nextCallback;
296299
}
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+
}

packages/scenejs/src/utils/property.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export function toPropertyObject(value: string | IObject<any> | any[], model?: N
167167
}
168168
return value;
169169
}
170+
170171
export function toObject(object: PropertyObject, result: IObject<any> = {}) {
171172
const model = object.model;
172173

packages/scenejs/test/unit/utils/color.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toFullHex, cutHex, hexToRGBA, hslToRGBA } from "@daybrush/utils";
2+
import { rgbaToHexa } from "../../../src/utils";
23

34
describe("color Test", () => {
45
describe("test color ", () => {
@@ -52,5 +53,26 @@ describe("color Test", () => {
5253
expect(rgb7).to.be.deep.equals([179, 77, 145, 0.4]);
5354
expect(rgb8).to.be.deep.equals([179, 77, 145, 1]);
5455
});
56+
it("should check 'rgbaToHexa'", () => {
57+
// Given, When
58+
const rgba1 = rgbaToHexa(`rgba(179, 77, 77, 1)`);
59+
const rgba2 = rgbaToHexa(`rgba(145, 179, 77, 1)`);
60+
const rgba3 = rgbaToHexa(`rgba(77, 179, 128, 1)`);
61+
const rgba4 = rgbaToHexa(`rgba(77, 119, 179, 1)`);
62+
const rgba5 = rgbaToHexa(`rgba(110, 77, 179, 1)`);
63+
const rgba6 = rgbaToHexa(`rgba(179, 77, 145, 1)`);
64+
const rgba7 = rgbaToHexa(`rgba(179, 77, 145, 0.4)`);
65+
const rgba8 = rgbaToHexa(`rgba(179, 77, 145, 1)`);
66+
67+
// Then
68+
expect(rgba1).to.be.equals("#b34d4dff");
69+
expect(rgba2).to.be.equals("#91b34dff");
70+
expect(rgba3).to.be.equals("#4db380ff");
71+
expect(rgba4).to.be.equals("#4d77b3ff");
72+
expect(rgba5).to.be.equals("#6e4db3ff");
73+
expect(rgba6).to.be.equals("#b34d91ff");
74+
expect(rgba7).to.be.equals("#b34d9166");
75+
expect(rgba8).to.be.equals("#b34d91ff");
76+
});
5577
});
5678
});

yarn.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,6 +3359,11 @@
33593359
resolved "https://registry.npmjs.org/@types/node/-/node-16.18.9.tgz"
33603360
integrity sha512-nhrqXYxiQ+5B/tPorWum37VgAiefi/wmfJ1QZKGKKecC8/3HqcTTJD0O+VABSPwtseMMF7NCPVT9uGgwn0YqsQ==
33613361

3362+
"@types/node@^18.11.17":
3363+
version "18.11.17"
3364+
resolved "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz#5c009e1d9c38f4a2a9d45c0b0c493fe6cdb4bcb5"
3365+
integrity sha512-HJSUJmni4BeDHhfzn6nF0sVmd1SMezP7/4F0Lq+aXzmp2xm9O7WXrUtHW/CHlYVtZUbByEvWidHqRtcJXGF2Ng==
3366+
33623367
"@types/normalize-package-data@^2.4.0":
33633368
version "2.4.1"
33643369
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz"
@@ -16328,6 +16333,17 @@ saxes@^5.0.1:
1632816333
dependencies:
1632916334
xmlchars "^2.2.0"
1633016335

16336+
scenejs@~1.8.5:
16337+
version "1.8.5"
16338+
resolved "https://registry.npmjs.org/scenejs/-/scenejs-1.8.5.tgz#0023581e64807dd7e802b601f4d88c4a727da0ff"
16339+
integrity sha512-LkiHyK2fNGrsV+68b7wQ76SirmgZR93vUsZOdXuWNxc+O9SlapnxkJF698w2ZYHNX331vL9ETUnbToF7zEEgXQ==
16340+
dependencies:
16341+
"@cfcs/core" "^0.0.12"
16342+
"@daybrush/utils" "^1.10.2"
16343+
"@scena/event-emitter" "^1.0.3"
16344+
css-styled "^1.0.0"
16345+
order-map "^0.2.2"
16346+
1633116347
scheduler@^0.23.0:
1633216348
version "0.23.0"
1633316349
resolved "https://registry.npmjs.org/scheduler/-/scheduler-0.23.0.tgz"

0 commit comments

Comments
 (0)