55import React from 'react'
66import clsx from 'clsx'
77
8- import { Avatar , Card , CardContent , CardHeader , styled , Tooltip } from '@mui/material'
8+ import { alpha , Avatar , Card , CardContent , CardHeader , getLuminance , getOverlayAlpha , styled , type Theme , Tooltip , useTheme } from '@mui/material'
99
1010import AppProjectIcon from 'icons/groups/AppProject'
1111import ComposerProjectIcon from 'icons/groups/ComposerProject'
1212
1313import { ContainerFlavors , GHOSTWIRE_LABEL_ROOT , type Project , projectDescription } from 'models/gw'
1414import { lighten } from '@mui/material'
15+ import { useImageLuminance } from 'hooks/imageluminance'
1516
1617import { ProjectContext } from './context'
1718
1819
20+ // Minimum contrast ratio between an app icon and its avatar background; see
21+ // also WCAG 2.x "non-text contrast".
22+ const MIN_ICON_CONTRAST = 3
23+
24+ /**
25+ * Returns true in dark mode if the specified icon luminance doesn't
26+ * sufficiently contrast with the theme's default background.
27+ */
28+ const needsLighterIconBackground = ( theme : Theme , iconLuminance : number | undefined ) => {
29+ if ( theme . palette . mode !== 'dark' || iconLuminance === undefined ) {
30+ return false
31+ }
32+ const bgLuminance = getLuminance ( theme . palette . background . default )
33+ const contrast = ( Math . max ( iconLuminance , bgLuminance ) + 0.05 )
34+ / ( Math . min ( iconLuminance , bgLuminance ) + 0.05 )
35+ return contrast < MIN_ICON_CONTRAST
36+ }
37+
38+ // Background overlay of an elevation 1 Paper in dark mode.
39+ const paperOverlay = `linear-gradient(${ alpha ( '#fff' , getOverlayAlpha ( 1 ) ) } , ${ alpha ( '#fff' , getOverlayAlpha ( 1 ) ) } )`
40+
41+
1942const ProjCard = styled ( Card ) ( ( { theme } ) => ( {
2043 backgroundColor : theme . palette . background . default ,
2144 borderStyle : 'dashed' ,
@@ -33,6 +56,10 @@ const ProjCard = styled(Card)(({ theme }) => ({
3356 borderStyle : "solid" ,
3457 backgroundColor : theme . palette . background . default ,
3558 } ,
59+ '&.flavor-ie-app .MuiAvatar-root.ieapp-icon.low-contrast' : {
60+ backgroundColor : theme . palette . background . paper ,
61+ backgroundImage : paperOverlay ,
62+ } ,
3663 '& > .MuiCardContent-root' : {
3764 paddingTop : 0 ,
3865 paddingBottom : theme . spacing ( 2 ) ,
@@ -72,6 +99,8 @@ const ProjectCard = ({ project, children }: ProjectCardProps) => {
7299 const ProjectIcon = isIEApp ? AppProjectIcon : ComposerProjectIcon
73100 const iconData = ( isIEApp && project . containers [ 0 ] . labels [ GHOSTWIRE_LABEL_ROOT + 'icon' ] ) || null
74101 const appTitle = project . containers [ 0 ] . labels [ GHOSTWIRE_LABEL_ROOT + 'ieapp/title' ]
102+ const theme = useTheme ( )
103+ const lowContrast = needsLighterIconBackground ( theme , useImageLuminance ( iconData ) )
75104
76105 return (
77106 < ProjCard
@@ -83,7 +112,11 @@ const ProjectCard = ({ project, children }: ProjectCardProps) => {
83112 avatar = {
84113 < Tooltip title = { projectDescription ( project ) } >
85114 { ( isIEApp && iconData )
86- ? < Avatar className = "ieapp-icon" variant = "rounded" src = { iconData } />
115+ ? < Avatar
116+ className = { clsx ( 'ieapp-icon' , lowContrast && 'low-contrast' ) }
117+ variant = "rounded"
118+ src = { iconData }
119+ />
87120 : < Avatar variant = "rounded" > < ProjectIcon /> </ Avatar >
88121 }
89122 </ Tooltip >
0 commit comments