Skip to content

Commit 1405566

Browse files
refactor: color int to css function (#18)
1 parent 5362e98 commit 1405566

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

src/Content/Embed/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import GifVEmbed from "./GifVEmbed";
22
import ImageEmbed from "./ImageEmbed";
33
import VideoAttachment from "../Attachment/VideoAttachment";
44
import * as Styles from "./style";
5-
import numberToRgb from "../../utils/numberToRgb";
5+
import { colorIntToRgba } from "../../utils/colorIntToCss";
66
import moment from "moment";
77
import { LinkMarkdown, parseEmbedTitle } from "../../markdown/render";
88
import useSize from "./useSize";
@@ -32,7 +32,7 @@ function Embed({ embed, images }: EmbedProps) {
3232

3333
const embedColor =
3434
embed.color !== 0 && embed.color !== undefined
35-
? numberToRgb(embed.color)
35+
? colorIntToRgba(embed.color)
3636
: undefined;
3737

3838
const { width: widthImage, height: heightImage } = useSize(

src/markdown/render/elements/mentions/roleMention.tsx

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from "react";
33
import type { Snowflake } from "discord-api-types/v10";
44
import { useConfig } from "../../../../core/ConfigContext";
55
import SimpleMarkdown from "simple-markdown";
6+
import { colorIntToRgba } from "../../../../utils/colorIntToCss";
67

78
interface RoleMentionProps {
89
roleId: Snowflake;
@@ -17,16 +18,12 @@ function RoleMention({ roleId }: RoleMentionProps) {
1718
return <>@deleted-role</>;
1819
}
1920

20-
const red = (role.color & 0xff_00_00) >> 16;
21-
const green = (role.color & 0x00_ff_00) >> 8;
22-
const blue = role.color & 0x00_00_ff;
23-
2421
return (
2522
<Styles.Mention
2623
css={{
27-
color: `rgb(${red}, ${green}, ${blue})`,
28-
"--mention-color": `rgba(${red}, ${green}, ${blue}, 0.1)`,
29-
"--mention-color-hover": `rgba(${red}, ${green}, ${blue}, 0.3)`,
24+
color: colorIntToRgba(role.color),
25+
"--mention-color": colorIntToRgba(role.color, 0.1),
26+
"--mention-color-hover": colorIntToRgba(role.color, 0.3),
3027
}}
3128
onClick={() => roleMentionOnClick?.(role)}
3229
canBeClicked={roleMentionOnClick !== undefined}

src/utils/colorIntToCss.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function colorIntToRgba(
2+
color: number,
3+
alpha = 1
4+
): `rgba(${number}, ${number}, ${number}, ${number})` {
5+
const r = (color >> 16) & 0xff;
6+
const g = (color >> 8) & 0xff;
7+
const b = color & 0xff;
8+
9+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
10+
}

src/utils/numberToRgb.ts

-9
This file was deleted.

0 commit comments

Comments
 (0)