首页 文章

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

提问于
浏览
4

目前我使用以下代码使用jss为元素添加颜色 .

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

我想知道是否存在一个基于颜色 theme.colors.red 添加不透明度的函数 .

smt例如: backgroundColor: color(theme.colors.red, .05),

5 回答

  • 0

    我找到了一个解决方案

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

    或者,您可以使用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;
    

    这里's how it'工作:https://github.com/mui-org/material-ui/blob/v1-beta/src/styles/colorManipulator.js#L157-L164

    另一种解决方案可能是使用https://github.com/styled-components/polished中的类似颜色函数

  • 4

    材质UI具有 colorManipulator utility file,其中包含 fade 方法:

    用法:

    import { fade } from '@material-ui/core/styles/colorManipulator';
    
    /**
     * Set 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()
     * @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: fade(theme.colors.red, 0.5)
    }
    

    color库也可能有用:

    import Color from 'color';
    

    然后你可以在你的代码中引用它:

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

    您可以使用RGBA值

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

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

  • 4

    对于它的 Value ,8位十六进制代码也可以工作

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

相关问题