Skip to content

Commit fd694bb

Browse files
committed
put geticontextcolor into profile functions
1 parent cac2e0f commit fd694bb

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

src/APIFunctions/Profile.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,37 @@ export function formatFirstAndLastName(user) {
1616
user.lastName.slice(1, user.lastName.length)
1717
);
1818
}
19+
20+
/**
21+
* Gets the appropriate icon text color for the
22+
* inputed background color
23+
* @param {String} color - The 6 character hex color code starting with #
24+
* @returns {String} The hex color of the icon text color
25+
*/
26+
export function getIconTextColor(color) {
27+
if(typeof color !== 'string') {
28+
throw new TypeError('color must be a string');
29+
}
30+
if(color == '' || color == '#2a323c') {
31+
return '#FFFFFF';
32+
}
33+
// get rgb values 0-255
34+
const r = parseInt(color.substring(1, 3), 16);
35+
const g = parseInt(color.substring(3, 5), 16);
36+
const b = parseInt(color.substring(5, 7), 16);
37+
// linearize the colors
38+
const colors = [r / 255.0, g / 255.0, b / 255.0];
39+
const c = colors.map((color) => {
40+
if(color <= 0.04045) {
41+
return color / 12.92;
42+
}
43+
return Math.pow(((color + 0.055) / 1.055), 2.4);
44+
});
45+
// luminance value
46+
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
47+
// threshold of 0.179
48+
if(L > 0.179) {
49+
return '#000000';
50+
}
51+
return '#FFFFFF';
52+
}

src/Components/Navbar/UserNavbar.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useUser } from '../context/UserContext';
44
import { useAuth } from '../context/AuthContext';
55
import { getUserById } from '../../APIFunctions/User';
66
import { useBackgroundColor } from '../context/BackgroundColorContext';
7+
import { getIconTextColor } from '../../APIFunctions/Profile';
78

89
export default function UserNavbar(props) {
910
const { user } = useUser();
@@ -37,35 +38,6 @@ export default function UserNavbar(props) {
3738
};
3839
}, [authenticated, user, backgroundColorVersion]);
3940

40-
// using w3c guidelines
41-
function getIconTextColor(color) {
42-
if(typeof color !== 'string') {
43-
throw new TypeError('color must be a string');
44-
}
45-
if(color == '' || color == '#2a323c') {
46-
return '#FFFFFF';
47-
}
48-
// get rgb values 0-255
49-
const r = parseInt(color.substring(1, 3), 16);
50-
const g = parseInt(color.substring(3, 5), 16);
51-
const b = parseInt(color.substring(5, 7), 16);
52-
// linearize the colors
53-
const colors = [r / 255.0, g / 255.0, b / 255.0];
54-
const c = colors.map((color) => {
55-
if(color <= 0.04045) {
56-
return color / 12.92;
57-
}
58-
return Math.pow(((color + 0.055) / 1.055), 2.4);
59-
});
60-
// luminance value
61-
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
62-
// threshold of 0.179
63-
if(L > 0.179) {
64-
return '#000000';
65-
}
66-
return '#FFFFFF';
67-
}
68-
6941
let initials = '';
7042
if (user && user.firstName && user.lastName) {
7143
initials = user.firstName[0] + user.lastName[0];

0 commit comments

Comments
 (0)