Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jss how to change opacity for a color

Currently I am using the following code to add a color to an element using jss.

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

I would like to know if exist a function to add opacity based on colortheme.colors.red.

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

like image 358
GibboK Avatar asked Nov 13 '17 16:11

GibboK


People also ask

How do you find the opacity of a color?

Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.

How do I change the color of the opacity in CSS?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).

Does opacity affect color?

Transparency—a common feature of modern web design—affects color appearance and so can add unwanted variation into a site's palette, but this change can be accounted for by calculating a new starting color.

How do I change the background color in material UI?

To set a background color on Material UI's Paper, you simply need to apply the background-color CSS property to the root element of the Paper. Setting the styles on the root element of any Material UI component can be done in multiple ways, but the most common is to use the useStyles hook.


2 Answers

Material UI has a colorManipulator utility file, which includes an alpha function:

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) } 

Alternatively, you can add the color library from npm for color manipulation:

import Color from 'color';  {     backgroundColor: Color(theme.colors.red).alpha(0.5).string() } 
like image 113
Craig Myles Avatar answered Sep 29 '22 08:09

Craig Myles


Alternatively, you can use the fade function provided in 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

Another solution could be to use similar color functions from https://github.com/styled-components/polished

like image 33
Romainpetit Avatar answered Sep 29 '22 08:09

Romainpetit