Skip to content

Commit 61c117a

Browse files
committed
added logic to change text color and hex validator
1 parent d1c484e commit 61c117a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/APIFunctions/Profile.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,24 @@ export function formatFirstAndLastName(user) {
1616
user.lastName.slice(1, user.lastName.length)
1717
);
1818
}
19+
20+
export function isValidHexColor(backgroundColor) {
21+
const hexAlphaRegex = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/;
22+
return hexAlphaRegex.test(backgroundColor);
23+
}
24+
25+
export function selectCorrectColor(backgroundColor) {
26+
let color = (backgroundColor.charAt(0) === '#') ? backgroundColor.substring(1, 7) : backgroundColor;
27+
let r = parseInt(color.substring(0, 2), 16); // hexToR
28+
let g = parseInt(color.substring(2, 4), 16); // hexToG
29+
let b = parseInt(color.substring(4, 6), 16); // hexToB
30+
let uicolors = [r / 255, g / 255, b / 255];
31+
let c = uicolors.map((col) => {
32+
if (col <= 0.03928) {
33+
return col / 12.92;
34+
}
35+
return Math.pow((col + 0.055) / 1.055, 2.4);
36+
});
37+
let isDark = (0.2126 * c[0]) + (0.7152 * c[1]) + (0.0722 * c[2]) <= 0.179;
38+
return isDark ? '#FFFFFF' : '#000000';
39+
}

src/Components/Navbar/UserNavbar.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { isValidHexColor, selectCorrectColor } from '../../APIFunctions/Profile';
23
import { membershipState } from '../../Enums';
34

45
export default function UserNavBar(props) {
@@ -7,11 +8,11 @@ export default function UserNavBar(props) {
78
initials = props.user.firstName[0] + props.user.lastName[0];
89
}
910

10-
let color = '';
11-
if(props.user.backgroundColor) {
12-
color = props.user.backgroundColor;
11+
let iconColor = '';
12+
if(isValidHexColor(props.user.backgroundColor)) {
13+
iconColor = props.user.backgroundColor;
1314
} else {
14-
color = '#15191e';
15+
iconColor = '#15191e';
1516
}
1617

1718
const unauthedRoutes = [
@@ -93,8 +94,8 @@ export default function UserNavBar(props) {
9394

9495
<div className="dropdown dropdown-bottom dropdown-end">
9596
<summary tabIndex={0} role="button" className="btn btn-ghost btn-circle avatar placeholder">
96-
<div className="w-12 rounded-full bg-neutral text-neutral-content" style={{backgroundColor: color}}>
97-
<span>{initials}</span>
97+
<div className="w-12 rounded-full bg-neutral text-neutral-content" style={{backgroundColor: iconColor }}>
98+
<span style={{color: selectCorrectColor(iconColor)}}>{initials}</span>
9899
</div>
99100
</summary>
100101
<div className='p-2 shadow menu dropdown-content z-[1] bg-base-100 w-52'>

src/Pages/UserManager/EditUserInfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function EditUserInfo(props) {
6262
new Date(result.responseData.membershipValidUntil)
6363
);
6464
setDiscordId(result.responseData.discordID);
65-
setBackgroundColor(result.responseData.backgroundColor || '#15191e');
65+
setBackgroundColor(result.responseData.backgroundColor);
6666
setEmail(result.responseData.email);
6767
}
6868
setLoading(false);

0 commit comments

Comments
 (0)