简体   繁体   English

Sass/Compass - 将 Hex、RGB 或命名颜色转换为 RGBA

[英]Sass/Compass - Convert Hex, RGB, or Named Color to RGBA

This may be Compass 101, but has anyone written a mixin which sets the alpha value of a color?这可能是 Compass 101,但是有没有人写过一个 mixin 来设置颜色的 alpha 值? Ideally, I would like the mixin to take any form of color definition, and apply transparency:理想情况下,我希望 mixin 采用任何形式的颜色定义,并应用透明度:

@include set-alpha( red, 0.5 );          //prints rgba(255, 0, 0, 0.5);
@include set-alpha( #ff0000, 0.5 );      //prints rgba(255, 0, 0, 0.5);
@include set-alpha( rgb(255,0,0), 0.5 ); //prints rgba(255, 0, 0, 0.5);

Use the rgba function built into Sass使用Sass 内置的rgba函数

Sets the opacity of a color.设置颜色的不透明度。

Examples:例子:

rgba(#102030, 0.5) => rgba(16, 32, 48, 0.5) rgba(#102030, 0.5) => rgba(16, 32, 48, 0.5)
rgba(blue, 0.2) => rgba(0, 0, 255, 0.2) rgba(蓝色, 0.2) => rgba(0, 0, 255, 0.2)

Parameters:参数:
(Color) color (颜色) 颜色
(Number) alpha — A number between 0 and 1 (数字) alpha — 0 到 1 之间的数字

Returns:回报:
(Color) (颜色)

Code:代码:

rgba(#ff0000, 0.5); // Output is rgba(255,0,0,0.5);

The rgba function doesn't work on color with no transparency, it returns an hex again. rgba 函数不适用于没有透明度的颜色,它再次返回一个十六进制。 After all, it's not meant to transform hex to rgba, we're just making profit out of hex doesn't allow alpha (yet).毕竟,这并不是要将 hex 转换为 rgba,我们只是从 hex 中获利,还不允许 alpha。

rgba(#fff, 1) // returns #fff

So, I made al little functions that buils the rgb string.因此,我制作了一些构建 rgb 字符串的小函数。 I don't need to deal with transparencies for now.我现在不需要处理透明胶片。

@function toRGB ($color) {
    @return "rgb(" + red($color) + ", " + green($color) + ", " + blue($color)+ ")";
}

I use the rgbapng compass plugin我使用rgbapng 指南针插件

rgbapng is a Compass plugin for providing cross-browser* compatible RGBA support. rgbapng 是一个 Compass 插件,用于提供跨浏览器* 兼容的 RGBA 支持。 It works by creating single pixel alpha-transparent PNGs on the fly for browsers that don't support RGBA.它通过为不支持 RGBA 的浏览器动态创建单像素 alpha 透明 PNG 来工作。 It uses the pure Ruby ChunkyPNG library resulting in hassle-free installation and deployment.它使用纯 Ruby ChunkyPNG 库,可轻松安装和部署。

Install安装

gem install compass-rgbapng

Usage用法

@include rgba-background(rgba(0,0,0,0.75));

Compiles to:编译为:

background: url('/images/rgbapng/000000bf.png?1282127952');
background: rgba(0, 0, 0, 0.75);

还有 ie-hex-str() 用于 IE 的 ##AARRGGBB 格式:

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#{ie-hex-str(#fdfdfd)}', endColorstr='#{ie-hex-str(#f6f6f6)}',GradientType=0); /* IE6-9 */
from_hex(hex_string, alpha = nil);

From the documentation :文档中:

Create a new color from a valid CSS hex string.从有效的 CSS 十六进制字符串创建新颜色。 The leading hash is optional.前导哈希是可选的。

SASS scale-color() will do this in a flexible way: eg color: #{scale-color( $primary, $alpha: -50% )}; SASS scale-color()将以灵活的方式执行此操作:例如color: #{scale-color( $primary, $alpha: -50% )}; . . Full reference here .完整参考这里

Note that if the initial color ( $primary in this example) is a solid color (with no transparency), then the $alpha value must be a negative value (up to -100% ) to add transparency.请注意,如果初始颜色(本例中为$primary )是纯色(没有透明度),则$alpha值必须为负值(最高-100% )才能增加透明度。

In my case, rgba doesn't allow hex.就我而言,rgba 不允许使用十六进制。

So, I use a transparentize which decreases the alpha channel.所以,我使用了一个减少 alpha 通道的透明化

.blue-background {
  background: transparentize(var.$color-my-blue, .8);
}

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

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