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),
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.
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).
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.
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.
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() }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With