Skip to content

Commit fef88db

Browse files
authored
Merge pull request #80 from siemens/develop
Develop
2 parents 35bb5dd + d2d77f4 commit fef88db

6 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎.devcontainer/fully-privileged/devcontainer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"postCreateCommand": {
5050
"node-for-all-with-yawn": "sudo ln -sf \"${NVM_BIN}/node\" /usr/local/bin/node",
51-
"yawn": ". ${NVM_DIR}/nvm.sh && cd webui && corepack enable && corepack enable && corepack install",
51+
"yawn": ". ${NVM_DIR}/nvm.sh && npm uninstall -g yarn pnpm && npm install -g corepack && cd webui && corepack enable && corepack install",
5252
"go-for-root": "echo 'Defaults:vscode secure_path = \"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin\"' | sudo tee -a /etc/sudoers.d/vscode",
5353
"nft": "sudo bash -c 'nft add table ip4 && nft add table nat && nft add table filter && nft add chain nat POSTROUTING'",
5454
"no-first-run-notice": "rm -f /workspaces/.codespaces/shared/first-run-notice.txt",

‎webui/.storybook/main.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const config: StorybookViteConfig = {
4040
disableWhatsNewNotifications: true,
4141
},
4242

43+
features: {
44+
// don't annoy with the "Get started" onboarding checklist in the
45+
// sidebar.
46+
sidebarOnboardingChecklist: false,
47+
},
48+
4349
typescript: {
4450
check: true,
4551
},

‎webui/src/components/projectcard/ProjectCard.stories.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,21 @@ export const IEAppProject: Story = {
5656
project: ieappProject,
5757
},
5858
}
59+
60+
// A dark app icon that gets a lighter avatar background in dark mode.
61+
const darkIcon = 'data:image/svg+xml,' + encodeURIComponent(
62+
'<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48">'
63+
+ '<path d="M12 8h24l-4 20h-16z" fill="#1e2a2a"/><rect x="18" y="30" width="12" height="10" fill="#1e2a2a"/>'
64+
+ '</svg>')
65+
66+
export const IEAppProjectWithDarkIcon: Story = {
67+
args: {
68+
project: {
69+
...ieappProject,
70+
containers: ieappProject.containers.map(cntr => ({
71+
...cntr,
72+
labels: { ...cntr.labels, [GHOSTWIRE_LABEL_ROOT + 'icon']: darkIcon },
73+
})),
74+
},
75+
},
76+
}

‎webui/src/components/projectcard/ProjectCard.tsx‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,40 @@
55
import React from 'react'
66
import 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

1010
import AppProjectIcon from 'icons/groups/AppProject'
1111
import ComposerProjectIcon from 'icons/groups/ComposerProject'
1212

1313
import { ContainerFlavors, GHOSTWIRE_LABEL_ROOT, type Project, projectDescription } from 'models/gw'
1414
import { lighten } from '@mui/material'
15+
import { useImageLuminance } from 'hooks/imageluminance'
1516

1617
import { 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+
1942
const 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>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// (c) Siemens AG 2026
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
import { useEffect, useState } from 'react'
6+
7+
// Images are downscaled to this size before sampling their pixels.
8+
const SAMPLE_SIZE = 32
9+
10+
// Converts an 8-bit sRGB channel value into linear light.
11+
const linear = (c: number) => {
12+
const v = c / 255
13+
return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4)
14+
}
15+
16+
/**
17+
* Returns the alpha-weighted mean relative luminance (0..1) of the specified
18+
* (loaded) image, or undefined if the image cannot be sampled or is fully
19+
* transparent.
20+
*/
21+
export const imageLuminance = (img: HTMLImageElement): number | undefined => {
22+
const canvas = document.createElement('canvas')
23+
canvas.width = SAMPLE_SIZE
24+
canvas.height = SAMPLE_SIZE
25+
const ctx = canvas.getContext('2d', { willReadFrequently: true })
26+
if (!ctx) {
27+
return undefined
28+
}
29+
ctx.drawImage(img, 0, 0, SAMPLE_SIZE, SAMPLE_SIZE)
30+
let data: Uint8ClampedArray
31+
try {
32+
data = ctx.getImageData(0, 0, SAMPLE_SIZE, SAMPLE_SIZE).data
33+
} catch {
34+
// tainted canvas due to a cross-origin image without CORS.
35+
return undefined
36+
}
37+
let sum = 0
38+
let weight = 0
39+
for (let i = 0; i < data.length; i += 4) {
40+
const a = data[i + 3] / 255
41+
if (a === 0) {
42+
continue
43+
}
44+
sum += a * (0.2126 * linear(data[i]) + 0.7152 * linear(data[i + 1]) + 0.0722 * linear(data[i + 2]))
45+
weight += a
46+
}
47+
return weight > 0 ? sum / weight : undefined
48+
}
49+
50+
/**
51+
* Hook returning the alpha-weighted mean relative luminance (0..1) of the
52+
* image at the specified URL, ignoring fully transparent pixels. Returns
53+
* undefined while the image is loading, or if its luminance cannot be
54+
* determined.
55+
*
56+
* @param src image URL, such as a data: URL.
57+
*/
58+
export const useImageLuminance = (src: string | null | undefined) => {
59+
const [luminance, setLuminance] = useState<{ src: string, value: number | undefined }>()
60+
61+
useEffect(() => {
62+
if (!src) {
63+
return
64+
}
65+
let cancelled = false
66+
const img = new Image()
67+
img.onload = () => {
68+
if (!cancelled) {
69+
setLuminance({ src, value: imageLuminance(img) })
70+
}
71+
}
72+
img.src = src
73+
return () => { cancelled = true }
74+
}, [src])
75+
76+
return luminance && luminance.src === src ? luminance.value : undefined
77+
}
78+
79+
export default useImageLuminance
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './imageluminance'

0 commit comments

Comments
 (0)