Twitter bootstrap hex to rgba? - css

Let a color variable used in sheet.less.
color is in HEX format #f57e20.
I want to use that color but add an alpha channel to it.
I want to end up with rgba(245, 126, 32, 0.5)
Does Bootstrap or less have anything to do this?

There are a some built in functions in less.js and mixins from Bootstrap that you could use:
This is a less.js function:
// using a variable
#color: #f57e20;
.test {
background: fade(#color, 50%); // return #color with 50% transparency
}
// or without the color variable
.test2 {
background: fade(#f57e20, 50%); // return #color with 50% transparency
}
These both result in:
.test {
background: rgba(245, 126, 32, 0.5);
}
.test2 {
background: rgba(245, 126, 32, 0.5);
}
Or use a Bootstrap mixin:
.test3 {
#translucent > .background(#f57e20, 0.5); // use HSLA mixin
}
Which results in:
.test3 {
background-color: rgba(244, 123, 31, 0.5);
}
I'll include the (fixed) code for the translucent mixins here for archiving purposes, since (last I checked) it is no longer included as of Bootstrap 3.0.0:
// Add an alphatransparency value to any background or border color (via Elyse Holladay)
#translucent {
.background(#color: #white, #alpha: 1) {
background-color: fade(#color, #alpha);
}
.border(#color: #white, #alpha: 1) {
border-color: fade(#color, #alpha);
.background-clip(padding-box);
}
}

You may want to try:
background: rgba(red(#color), green(#color), blue(#color), #alpha);
I had to do something similar when writing a quick mixin which used box-shadow.

Related

how can I use a Saas var with three color values like $green: 60, 187, 180 so later background-color: rgba($green, 0.8)?

I am trying to use a Saas var called $green:
$green: 60, 187, 180; // 60, 187, 180 are my values in rgb
Later I am trying use this color to include in background-color propery:
.icon-button[aria-label="thumbs up"] {
background-color: rgba($green, 0.8);
}
.icon-button[aria-label="thumbs up"]:hover {
background-color: rgba($green, 1);
}
I am getting "SassError: $color: 60, 187, 180 is not a color." who can I fix it?
thanks..
With DART SASS you can use the color build-in module.
Sass: sass:color
You could use the scale function:
color.scale($green, $alpha: -20%)
But note that in this example the opacity is reduced by 20% (1 - 0.2 = 0.8). So you can reduce/increase the opacity by this value (not set it to the desired value).
So a solution would be:
This SASS
$green: rgb(60, 187, 180); // 60, 187, 180 are my values in rgb
.icon-button[aria-label="thumbs up"] {
background-color: color.scale($green, $alpha: -20%);
}
.icon-button[aria-label="thumbs up"]:hover {
background-color: color.scale($green, $alpha: 0%);
}
becomes this CSS:
.icon-button[aria-label="thumbs up"] {
background-color: rgba(60, 187, 180, 0.8);
}
.icon-button[aria-label="thumbs up"]:hover {
background-color: #3cbbb4;
}
EDIT:
Another approach would be to use CSS variables. So, dynamic transparency values ​​must be inserted into the RGB color values ​​using SASS functions and / or mixins and then reassembled into a RGBA string.
I followed this approach to allow the user to add their own themes to a system without having to use SASS themselves.
In this code snipped from me you can perhaps guess what it might look like later. CSS: CSS Variable is not defined in pseudo element

How to change side menu background color in ionic 4?

I want to change the side menu background color, I tried a few things but not working.
ion-menu{
ion-content{
background-color: transparent !important;
}
.inner-scroll {
--background: var(--ion-menu-background);
}
}
Does anyone know how I can change background color in ionic 4?.
Try this
.inner-scroll {
background: green;
}
.inner-scroll .item-native{
background: green;
}
In cases where there are gradients and shades of colors, you can try this. This is particularly useful if you want the entire side-menu to have a gradient, without any borders, separating the toolbar, content and items.
// In variable.scss file you can create your Ionic css variables
:root
{
--custom-menu: radial-gradient(
circle farthest-corner at 10% 20%,
rgba(246, 133, 133, 1) 16.3%,
rgba(172, 131, 241, 1) 90%
);
}
// In the app.component.scss file you would need to reference the variable that you created
ion-menu {
--ion-background-color: var(--custom-menu);
ion-toolbar {
--background: transparent;
}
ion-list {
background: transparent;
}
ion-item {
color: rgb(255, 255, 255);
--background: transparent;
}
ion-content {
--background: transparent;
}
}
// In the appcomponent.html file, ensure that you specify the no-border directive
<ion-header no-border>
<ion-toolbar>
<ion-title>Test</ion-title>
</ion-toolbar>
</ion-header>
This is the output

Change Atom's "git diff" package theme to Github theme

The default theme for Atom's Git Diff package is,
It shows the changes in dark green, deleted lines in dark red and has a black background.
Is there a way to show the difference as shown in the Github console as show in the image below.
I tried adding the code below to the styles.less file but that does not highlight the modified text and does not modify the colour in the unstaged changes but modifies the colour while working on the file.(please refer to image below the code)
atom-text-editor .gutter .line-number {
&[class*="git"] {
padding-left: 0 !important;
border-left: none !important;
color: #ffffff;
opacity: 1;
}
&.git-line-added {
background-color: rgba(168, 255, 96, 0.6);
}
&.git-line-modified {
background-color: rgba(233, 192, 98, 0.6);
}
&.git-line-removed {
background-color: rgba(204, 102, 102, 0.6);
}}

Use CSS variables with rgba for gradient transparency

Is there a way to use CSS variables when specifying gradient colors with transparency, e.g.
:root {
--accent-color: #dfd0a5;
}
h1{
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(red(var(--accent-color)), green(var(--accent-color)), blue(var(--accent-color)), 1));
}
You can use variables, but you can't sample the individual red, green and blue components from a single hex value in CSS.
If you're simply looking to apply an alpha component to an existing RGB triplet, you can specify the entire triplet as a comma-separated list of decimal values instead of a hex value, and substitute it directly into the rgba() function as a single opaque token:
:root {
--accent-color: 223, 208, 165;
}
h1 {
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(var(--accent-color), 1));
}
If you want to specify and control individual R, G and B values with rgba(), you will need to specify a variable for each color component as a decimal value, and reference each variable within the rgba() function like so:
:root {
--accent-red: 223;
--accent-green: 208;
--accent-blue: 165;
}
h1 {
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(var(--accent-red), var(--accent-green), var(--accent-blue), 1));
}
#boltclock said it all, but you can save a bit of time if your project has a scss preprocessor.
You can do a little tweak to achieve what you want :
// Scss
#mixin defineColorRGB ($color, $red, $green, $blue) {
#{$color}: unquote("rgb(#{$red}, #{$green}, #{$blue})");
#{$color}-r: #{$red};
#{$color}-g: #{$green};
#{$color}-b: #{$blue};
}
Then in you css, you can do this:
::root {
#include defineColorRGB(--accentColor, red(#dfd0a5), green(#dfd0a5), blue(#dfd0a5));
}
You will end up with 4 different css variables, one for your color, and one for each color channel.
Then you can use it almost like you wrote it:
h1{
background: linear-gradient(
to right, rgba(255, 255, 255, 0),
rgba(var(--accent-color-r), var(--accent-color-g), var(--accent-color-b), 1)
);
}
I find it a very convenient way to initialize my css variables, and use it in most of my projects.

How do I convert a hexadecimal color to rgba with the Less compiler?

I have the following code:
#color : #d14836;
.stripes span {
-webkit-background-size: 30px 30px;
-moz-background-size: 30px 30px;
background-size: 30px 30px;
background-image: -webkit-gradient(linear, left top, right bottom,
color-stop(.25, rgba(209, 72, 54, 1)), color-stop(.25, transparent),
color-stop(.5, transparent), color-stop(.5, rgba(209, 72, 54, 1)),
color-stop(.75, rgba(209, 72, 54, 1)), color-stop(.75, transparent),
to(transparent));
I need to convert #color to rgba(209, 72, 54, 1).
So I need to replace rgba(209, 72, 54, 1) in my code with a Less function that generates an rgba() value from my #color variable.
How can I do this with Less?
Actually, the Less language comes with an embedded function called fade. You pass a color object and the absolute opacity % (higher value means less transparent):
fade(#color, 50%); // Return #color with 50% opacity in rgba
If you don't need an alpha key, you can simply use the hexadecimal representation of the color. An rgba color with a alpha of '1' is the same as the hexadecimal value.
Here are some examples to demonstrate that:
#baseColor: #d14836;
html {
color: #baseColor;
/* color:#d14836; */
}
body {
color: rgba(red(#baseColor), green(#baseColor), blue(#baseColor), 1);
/* color:#d14836; */
}
div {
color: rgba(red(#baseColor), green(#baseColor), blue(#baseColor), 0.5);
/* rgba(209, 72, 54, 0.5); */
}
span {
color: fade(#baseColor, 50%);
/* rgba(209, 72, 54, 0.5); */
}
h3 {
color: fade(#baseColor, 100%)
/* color:#d14836; */
}
Test this code online: http://lesstester.com/
My Less mixin:
.background-opacity(#color, #opacity) {
#rgba-color: rgba(red(#color), green(#color), blue(#color), #opacity);
background-color: #rgba-color;
// Hack for IE8:
background: none\9; // Only Internet Explorer 8
filter: e(%("progid:DXImageTransform.Microsoft.gradient(startColorstr='%d', endColorstr='%d')", argb(#rgba-color),argb(#rgba-color))); // Internet Explorer 9 and down
// Problem: Filter gets applied twice in Internet Explorer 9.
// Solution:
&:not([dummy]) {
filter: progid:DXImageTransform.Microsoft.gradient(enabled='false'); // Only IE9
}
}
Try it.
EDITED:
As seen on rgba background with IE filter alternative: IE9 renders both!, I added some lines to the mixin.
It seems that with the recent Less update 3.81, you can use just two arguments in the rgba() function.
rgba(white, 0.3) or rgba(white, 30%) => rgba(255, 255, 255, 0.3)
It works for me, but I can't find it in the official documentation.

Resources