|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { Tooltip } from '@deephaven/components'; |
| 3 | +import { ColorUtils } from '@deephaven/utils'; |
| 4 | +import palette from '@deephaven/components/src/theme/theme-dark/theme-dark-palette.css?inline'; |
| 5 | +import semantic from '@deephaven/components/src/theme/theme-dark/theme-dark-semantic.css?inline'; |
| 6 | +import semanticEditor from '@deephaven/components/src/theme/theme-dark/theme-dark-semantic-editor.css?inline'; |
| 7 | +import semanticGrid from '@deephaven/components/src/theme/theme-dark/theme-dark-semantic-grid.css?inline'; |
| 8 | +import styles from './ThemeColors.module.scss'; |
| 9 | + |
| 10 | +// Group names are extracted from var names via a regex capture group. Most of |
| 11 | +// them work pretty well, but some need to be remapped to a more appropriate |
| 12 | +// group. |
| 13 | +const reassignVarGroups: Record<string, string> = { |
| 14 | + '--dh-color-black': 'gray', |
| 15 | + '--dh-color-white': 'gray', |
| 16 | + // Editor |
| 17 | + '--dh-color-editor-bg': 'editor', |
| 18 | + '--dh-color-editor-fg': 'editor', |
| 19 | + '--dh-color-editor-context-menu-bg': 'menus', |
| 20 | + '--dh-color-editor-context-menu-fg': 'menus', |
| 21 | + '--dh-color-editor-menu-selection-bg': 'menus', |
| 22 | + // Grid |
| 23 | + '--dh-color-grid-bg': 'grid', |
| 24 | + '--dh-color-grid-number-positive': 'Data Types', |
| 25 | + '--dh-color-grid-number-negative': 'Data Types', |
| 26 | + '--dh-color-grid-number-zero': 'Data Types', |
| 27 | + '--dh-color-grid-date': 'Data Types', |
| 28 | + '--dh-color-grid-string-null': 'Data Types', |
| 29 | +}; |
| 30 | + |
| 31 | +// Mappings of variable groups to rename |
| 32 | +const renameGroups = { |
| 33 | + editor: { |
| 34 | + line: 'editor', |
| 35 | + comment: 'code', |
| 36 | + string: 'code', |
| 37 | + number: 'code', |
| 38 | + delimiter: 'code', |
| 39 | + identifier: 'code', |
| 40 | + keyword: 'code', |
| 41 | + operator: 'code', |
| 42 | + storage: 'code', |
| 43 | + predefined: 'code', |
| 44 | + selection: 'state', |
| 45 | + focus: 'state', |
| 46 | + }, |
| 47 | + grid: { data: 'Data Bars', context: 'Context Menu' }, |
| 48 | +}; |
| 49 | + |
| 50 | +export function ThemeColors(): JSX.Element { |
| 51 | + const swatchDataGroups = useMemo( |
| 52 | + () => ({ |
| 53 | + 'Theme Color Palette': buildColorGroups(palette, 1), |
| 54 | + 'Semantic Colors': buildColorGroups(semantic, 1), |
| 55 | + 'Editor Colors': buildColorGroups(semanticEditor, 2, renameGroups.editor), |
| 56 | + 'Grid Colors': buildColorGroups(semanticGrid, 2, renameGroups.grid), |
| 57 | + }), |
| 58 | + [] |
| 59 | + ); |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + {Object.entries(swatchDataGroups).map(([label, data]) => ( |
| 64 | + <div key={label}> |
| 65 | + <h2 className="ui-title">{label}</h2> |
| 66 | + <div className={styles.themeColors}> |
| 67 | + {Object.entries(data).map(([group, swatchData]) => ( |
| 68 | + <div |
| 69 | + key={group} |
| 70 | + // This is the secret sauce for filling columns. The height of |
| 71 | + // each swatch group spans multiple rows (the number of swatches |
| 72 | + // + 1 for the label). This causes the grid to create rows |
| 73 | + // based on the swatch height (35px), and each swatch (also the |
| 74 | + // group label) neatly fits in a grid cell. The grid will put a |
| 75 | + // group in each column and then wrap back around to the first |
| 76 | + // until all groups are placed. |
| 77 | + style={{ gridRow: `span ${swatchData.length + 1}` }} |
| 78 | + > |
| 79 | + <span className={styles.label}>{group}</span> |
| 80 | + {swatchData.map(({ name, value }) => ( |
| 81 | + <div |
| 82 | + key={name} |
| 83 | + className={styles.swatch} |
| 84 | + style={{ |
| 85 | + backgroundColor: value, |
| 86 | + color: `var(--dh-color-${contrastColor(value)})`, |
| 87 | + }} |
| 88 | + > |
| 89 | + <Tooltip> |
| 90 | + <div>{name}</div> |
| 91 | + <div>{value}</div> |
| 92 | + <div> |
| 93 | + {ColorUtils.normalizeCssColor(value).replace( |
| 94 | + /^(#[a-f0-9]{6})ff$/, |
| 95 | + '$1' |
| 96 | + )} |
| 97 | + </div> |
| 98 | + </Tooltip> |
| 99 | + <span>{name.replace('--dh-color-', '')}</span> |
| 100 | + {name.endsWith('-hue') ? <span>{value}</span> : null} |
| 101 | + </div> |
| 102 | + ))} |
| 103 | + </div> |
| 104 | + ))} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ))} |
| 108 | + </> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +export default ThemeColors; |
| 113 | + |
| 114 | +/** Return black or white contrast color */ |
| 115 | +function contrastColor(color: string): 'black' | 'white' { |
| 116 | + const rgba = ColorUtils.parseRgba(ColorUtils.asRgbOrRgbaString(color) ?? ''); |
| 117 | + if (rgba == null || rgba.a < 0.5) { |
| 118 | + return 'white'; |
| 119 | + } |
| 120 | + |
| 121 | + const { r, g, b } = rgba; |
| 122 | + const y = (299 * r + 587 * g + 114 * b) / 1000; |
| 123 | + return y >= 128 ? 'black' : 'white'; |
| 124 | +} |
| 125 | + |
| 126 | +/** Extract an array of { name, value } pairs for css variables in a given string */ |
| 127 | +function extractColorVars( |
| 128 | + styleText: string |
| 129 | +): { name: string; value: string }[] { |
| 130 | + const computedStyle = getComputedStyle(document.documentElement); |
| 131 | + |
| 132 | + return styleText |
| 133 | + .split('\n') |
| 134 | + .map(line => /^\s{2}(--dh-color-(?:[^:]+))/.exec(line)?.[1]) |
| 135 | + .filter(Boolean) |
| 136 | + .map(varName => |
| 137 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 138 | + ({ name: varName!, value: computedStyle.getPropertyValue(varName!)! }) |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +/** Group color data based on capture group value */ |
| 143 | +function buildColorGroups( |
| 144 | + styleText: string, |
| 145 | + captureGroupI: number, |
| 146 | + groupRemap: Record<string, string> = {} |
| 147 | +): Record<string, { name: string; value: string }[]> { |
| 148 | + const swatchData = extractColorVars(styleText); |
| 149 | + |
| 150 | + const groupData = swatchData.reduce( |
| 151 | + (acc, { name, value }) => { |
| 152 | + const match = /^--dh-color-([^-]+)(?:-([^-]+))?/.exec(name); |
| 153 | + let group = |
| 154 | + reassignVarGroups[name] ?? |
| 155 | + match?.[captureGroupI] ?? |
| 156 | + match?.[1] ?? |
| 157 | + '???'; |
| 158 | + |
| 159 | + group = groupRemap[group] ?? group; |
| 160 | + |
| 161 | + if (acc[group] == null) { |
| 162 | + acc[group] = []; |
| 163 | + } |
| 164 | + |
| 165 | + // Add a spacer for black / white |
| 166 | + if (name === '--dh-color-black') { |
| 167 | + acc[group].push({ name: '', value: '' }); |
| 168 | + } |
| 169 | + |
| 170 | + acc[group].push({ name, value }); |
| 171 | + |
| 172 | + return acc; |
| 173 | + }, |
| 174 | + {} as Record<string, { name: string; value: string }[]> |
| 175 | + ); |
| 176 | + |
| 177 | + return groupData; |
| 178 | +} |
0 commit comments