简体   繁体   English

jss如何改变颜色的不透明度

[英]jss how to change opacity for a color

Currently I am using the following code to add a color to an element using jss.目前我正在使用以下代码使用 jss 为元素添加颜色。

 const styleSheet = theme => ({ root: { backgroundColor: theme.colors.red, }, })

I would like to know if exist a function to add opacity based on color theme.colors.red .我想知道是否存在基于颜色theme.colors.red添加不透明度的功能。

example smt like: backgroundColor: color(theme.colors.red, .05),示例 smt 像: backgroundColor: color(theme.colors.red, .05),

Material UI has a colorManipulator utility file , which includes an alpha function: Material UI 有一个colorManipulator 实用程序文件,其中包含一个alpha函数:

import { alpha } from '@material-ui/core/styles/colorManipulator';

/**
 * Sets the absolute transparency of a color.
 * Any existing alpha values are overwritten.
 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla(), color()
 * @param {number} value - value to set the alpha channel to in the range 0 - 1
 * @returns {string} A CSS color string. Hex input values are returned as rgb
 */

{
    backgroundColor: alpha(theme.colors.red, 0.5)
}

For Mui v5:对于 Mui v5:

import { alpha } from "@mui/material";

Alternatively, you can add the color library from npm for color manipulation:或者,您可以从 npm 添加颜色库以进行颜色操作:

import Color from 'color';

{
    backgroundColor: Color(theme.colors.red).alpha(0.5).string()
}

Alternatively, you can use the fade function provided in Material UI Next.或者,您可以使用 Material UI Next 中提供的淡入淡出功能。

import {fade} from 'material-ui/styles/colorManipulator';

const theme = createMuiTheme({
  overrides: {
    MuiButton: {
      root: {
        boxShadow: `0 4px 8px 0 ${fade(defaultTheme.palette.primary[500], 0.18)}`,
      }
    },
  }
});

export default theme;

Here's how it's working : https://github.com/mui-org/material-ui/blob/v1-beta/src/styles/colorManipulator.js#L157-L164这是它的工作原理: https ://github.com/mui-org/material-ui/blob/v1-beta/src/styles/colorManipulator.js#L157-L164

Another solution could be to use similar color functions from https://github.com/styled-components/polished另一种解决方案可能是使用来自https://github.com/styled-components/poliished的类似颜色函数

Assuming you don't already have the alpha channel defined in the color, you can also do:假设您还没有在颜色中定义 alpha 通道,您还可以执行以下操作:

backgroundColor: theme.colors.red + '00' 

This will set alpha channel to 0, thus transparent.这会将 Alpha 通道设置为 0,因此是透明的。 You can append any value between '00' to 'ff'您可以附加'00''ff'之间的任何值

我找到了一个解决方案

 backgroundColor: theme.utils.rgba(theme.axColor.black, 0.7),

Some of these answers are referencing deprecated Material-UI functions.其中一些答案引用了已弃用的 Material-UI 函数。 The current preferred approach is to use alpha :当前首选的方法是使用alpha

import { alpha } from "@material-ui/core";

...
                 // yields rgba(255,255,255,0.85)
backgroundColor: alpha(theme.palette.background.paper, 0.85) 

You can use RGBA values您可以使用 RGBA 值

const styleSheet = theme => ({
  root: {
     backgroundColor: 'rgba(255, 255, 255, 0.5)',
  },
})

https://facebook.github.io/react-native/docs/colors.html https://facebook.github.io/react-native/docs/colors.html

Another possibility is:另一种可能是:

import color from "color"

const themeColorsRed = color
  .rgb(theme.colors.red)
  .array()

Then you can do:然后你可以这样做:

{
  backgroundColor: `rgba(${themeColorsRed}, 0.05)`,
}

For what it's worth, an 8 digit hex code works too值得一提的是,8 位十六进制代码也可以使用

const styleSheet = theme => ({
  root: {
     backgroundColor: '#ffffff80',
  },
})

for MUI v5 this seems to work:对于 MUI v5,这似乎可行:

import { alpha } from '@mui/material';

... ...

MuiContainer: {
      styleOverrides: {
        root: {
          '&.MuiContainer-asideWithImage': {
            backgroundColor: alpha(MY_COLOR, 0.78),
          },
        },
      },
    },

... ...

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

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