简体   繁体   English

如何在 JS/SCSS 中生成 Material 3 调色板?

[英]How to Generate Material 3 Color Palettes in JS/SCSS?

Background:背景:

I'm trying to generate a color palette in SASS/JS based on Google's Material Theme Builder 3 which requires tonal palette's to be generated based on relative luminance and not lightness/brightness function.我正在尝试基于Google 的 Material Theme Builder 3在 SASS/JS 中生成调色板,该调色板需要根据相对亮度而不是亮度/亮度 function 生成调色板。

在此处输入图像描述

Problem:问题:

I can get the luminance value using the following function in SCSS:我可以在 SCSS 中使用以下 function 获得亮度值:

@function get-luminance($color) {
    $colors: (
        'red': red($color),
        'green': green($color),
        'blue': blue($color),
    );

    @each $name, $value in $colors {
        $adjusted: 0;
        $value: $value / 255;

        @if $value < 0.03928 {
            $value: $value / 12.92;
        } @else {
            $value: ($value + 0.055) / 1.055;
            $value: math.pow($value, 2.4);
        }

        $colors: map-merge($colors, ($name: $value));
    }

    @return (map-get($colors, 'red') * 0.2126) + (map-get($colors, 'green') * 0.7152) + (map-get($colors, 'blue') * 0.0722);
}

But what I'm looking for is to create a function that can adjust a certain color's luminance eg但我正在寻找的是创建一个 function 可以调整某种颜色的亮度,例如

@function adjust-luminance($color, $luminance-value) {
    // Calculations required to adjust luminance here
    @return $adjusted-luminance-color;
}

$seed-color: #6750A4;

.color-tone-99 {
    background: adjust-luminance($seed-color, 97.4); // Output: #FFFBFE
}

I haven't been able to figure out the calculations part above.我无法弄清楚上面的计算部分。 I've also come across this Color Luminance Figma plugin that does it in Figma but how does it do it is the question.我也遇到过这个在 Figma 中实现的Color Luminance Figma 插件,但它是如何实现的却是个问题。

Any help would be highly appreciated!任何帮助将不胜感激!

Thanks谢谢

Material 3 uses a new system called HCT:材料 3 使用称为 HCT 的新系统:

It turns out James O'Leary, Color Scientist, Platforms & Ecosystems at Google developed a brand new, perceptually accurate, color system is called HCT, which stands for hue, chroma, tone.事实证明,Google 的平台和生态系统色彩科学家 James O'Leary 开发了一种全新的、感知准确的色彩系统,称为 HCT,它代表色相、色度和色调。

He has written a great article explaining why and how it was created here: The Science of Color & Design .他写了一篇很棒的文章来解释为什么以及如何在这里创建它:色彩与设计科学

The Material Foundation has also released their codebase in TypeScript here: Material Color Utilities GitHub Repo . Material Foundation 还在 TypeScript 中发布了他们的代码库: Material Color Utilities GitHub Repo

How To Generate Material 3 Color Palette in JavaScript:如何在 JavaScript 中生成材料 3 调色板:

1. Install the Material Color Utilities via NPM: 1. 通过 NPM 安装 Material Color Utilities:

npm i @material/material-color-utilities

2. Import the Utilities: 2. 导入实用程序:

import { argbFromHex, hexFromArgb, themeFromSourceColor, TonalPalette } from '@material/material-color-utilities';

3. Getting Theme JSON: 3.获取主题JSON:

const m3ThemeColorsJSON = themeFromSourceColor(argbFromHex('#6750A4'), []);

4. This will return a JSON map of ARGB colors, to convert them to HEX colors, use the hexFromArgb() function. 4. This will return a JSON map of ARGB colors, to convert them to HEX colors, use the hexFromArgb() function. Example:例子:

const primary = hexFromArgb(theme.schemes.light.primary);

5. If you want to get a color from the Tonal Palette, use the following: 5. 如果要从 Tonal Palette 中获取颜色,请使用以下命令:

const primary98 = TonalPalette.fromInt(primary).tone(98);

Support in SCSS: SCSS 中的支持:

The Material Foundation intends to release a version in SCSS in the future but anybody can use this in JavaScript to generate an SCSS file in the meantime. Material Foundation 打算在未来发布一个 SCSS 版本,但同时任何人都可以在 JavaScript 中使用它来生成 SCSS 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM