Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing schema for typescript #127

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

bre97-web
Copy link

@bre97-web bre97-web commented Nov 17, 2023

Surface Containers

  • surfaceContainerLowest
  • surfaceContainerLow
  • surfaceContainer
  • surfaceContainerHigh
  • surfaceContainerHighest

Add-ons

  • primaryFixed
  • onPrimaryFixed
  • primaryFixedDim
  • onPrimaryFixedVariant
  • secondaryFixed
  • onSecondaryFixed
  • secondaryFixedDim
  • onSecondaryFixedVariant
  • tertiaryFixed
  • onTertiaryFixed
  • tertiaryFixedDim
  • onTertiaryFixedVariant
  • surfaceBright
  • surfaceDim
  • surfaceTint

Fix values of background, surface and onErrorContainer.

@AnCIity
Copy link

AnCIity commented Feb 22, 2024

Why hasn't this pr been processed yet?

PiperOrigin-RevId: 582616798
@bre97-web
Copy link
Author

bre97-web commented Feb 23, 2024

I saw someone wondering after calling themeFromSourceColor and then applyTheme, why are the colors such as surfaceContainer missing? If you need to temporarily solve this problem, you can consider the following code:

import { Hct, MaterialDynamicColors, SchemeContent, argbFromHex, hexFromArgb } from "@material/material-color-utilities"

const materialColors = {
    primaryPaletteKeyColor: MaterialDynamicColors.primaryPaletteKeyColor,
    secondaryPaletteKeyColor: MaterialDynamicColors.secondaryPaletteKeyColor,
    tertiaryPaletteKeyColor: MaterialDynamicColors.tertiaryPaletteKeyColor,
    neutralPaletteKeyColor: MaterialDynamicColors.neutralPaletteKeyColor,
    neutralVariantPaletteKeyColor: MaterialDynamicColors.neutralVariantPaletteKeyColor,
    background: MaterialDynamicColors.background,
    onBackground: MaterialDynamicColors.onBackground,
    surface: MaterialDynamicColors.surface,
    surfaceDim: MaterialDynamicColors.surfaceDim,
    surfaceBright: MaterialDynamicColors.surfaceBright,
    surfaceContainerLowest: MaterialDynamicColors.surfaceContainerLowest,
    surfaceContainerLow: MaterialDynamicColors.surfaceContainerLow,
    surfaceContainer: MaterialDynamicColors.surfaceContainer,
    surfaceContainerHigh: MaterialDynamicColors.surfaceContainerHigh,
    surfaceContainerHighest: MaterialDynamicColors.surfaceContainerHighest,
    onSurface: MaterialDynamicColors.onSurface,
    surfaceVariant: MaterialDynamicColors.surfaceVariant,
    onSurfaceVariant: MaterialDynamicColors.onSurfaceVariant,
    inverseSurface: MaterialDynamicColors.inverseSurface,
    inverseOnSurface: MaterialDynamicColors.inverseOnSurface,
    outline: MaterialDynamicColors.outline,
    outlineVariant: MaterialDynamicColors.outlineVariant,
    shadow: MaterialDynamicColors.shadow,
    scrim: MaterialDynamicColors.scrim,
    surfaceTint: MaterialDynamicColors.surfaceTint,
    primary: MaterialDynamicColors.primary,
    onPrimary: MaterialDynamicColors.onPrimary,
    primaryContainer: MaterialDynamicColors.primaryContainer,
    onPrimaryContainer: MaterialDynamicColors.onPrimaryContainer,
    inversePrimary: MaterialDynamicColors.inversePrimary,
    secondary: MaterialDynamicColors.secondary,
    onSecondary: MaterialDynamicColors.onSecondary,
    secondaryContainer: MaterialDynamicColors.secondaryContainer,
    onSecondaryContainer: MaterialDynamicColors.onSecondaryContainer,
    tertiary: MaterialDynamicColors.tertiary,
    onTertiary: MaterialDynamicColors.onTertiary,
    tertiaryContainer: MaterialDynamicColors.tertiaryContainer,
    onTertiaryContainer: MaterialDynamicColors.onTertiaryContainer,
    error: MaterialDynamicColors.error,
    onError: MaterialDynamicColors.onError,
    errorContainer: MaterialDynamicColors.errorContainer,
    onErrorContainer: MaterialDynamicColors.onErrorContainer,
    primaryFixed: MaterialDynamicColors.primaryFixed,
    primaryFixedDim: MaterialDynamicColors.primaryFixedDim,
    onPrimaryFixed: MaterialDynamicColors.onPrimaryFixed,
    onPrimaryFixedVariant: MaterialDynamicColors.onPrimaryFixedVariant,
    secondaryFixed: MaterialDynamicColors.secondaryFixed,
    secondaryFixedDim: MaterialDynamicColors.secondaryFixedDim,
    onSecondaryFixed: MaterialDynamicColors.onSecondaryFixed,
    onSecondaryFixedVariant: MaterialDynamicColors.onSecondaryFixedVariant,
    tertiaryFixed: MaterialDynamicColors.tertiaryFixed,
    tertiaryFixedDim: MaterialDynamicColors.tertiaryFixedDim,
    onTertiaryFixed: MaterialDynamicColors.onTertiaryFixed,
    onTertiaryFixedVariant: MaterialDynamicColors.onTertiaryFixedVariant,
}

export function generateStyleTextBySourceColor(argb: number, isDark: boolean, contrastLevel: number) {
    const scheme = new SchemeContent(Hct.fromInt(argb), isDark, contrastLevel)

    /**
     * ...
     * [onSurface, MaterialDynamicColors.onSurface] -> --md-sys-color-on-surface: #ffffff;
     * ...
     */
    return Object.entries(materialColors).map(e => `--md-sys-color-${toKebabCase(e[0])}: ${hexFromArgb(e[1].getArgb(scheme))};`).reduce((l, c) => l + c)
}

function toKebabCase(str: string) {
    return str.split('').map((letter, idx) => {
        return letter.toUpperCase() === letter
            ? `${idx !== 0 ? '-' : ''}${letter.toLowerCase()}`
            : letter
    }).join('')
}

const styleText = generateStyleTextBySourceColor(argbFromHex('#fe8056'), false, 0);
document.body.setAttribute('style', styleText)

The document.body style:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants