Sass/Compass - 将十六进制、RGB 或命名颜色转换为 RGBA
这可能是 Compass 101,但是有人写过一个 mixin 来设置颜色的 alpha 值吗?理想情况下,我希望 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);
This may be Compass 101, but has anyone written a mixin which sets the alpha value of a color? Ideally, I would like the mixin to take any form of color definition, and apply transparency:
@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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 Sass 中内置的
rgba
函数代码:
Use the
rgba
function built into SassCode:
rgba 函数不适用于没有透明度的颜色,它再次返回十六进制。毕竟,这并不是要将十六进制转换为 rgba,我们只是从不允许 alpha 的十六进制中获利。
所以,我创建了一些构建 RGB 字符串的小函数。我现在不需要处理透明胶片。
The rgba function doesn't work on color with no transparency, it returns an hex again. After all, it's not meant to transform hex to rgba, we're just making profit out of hex doesn't allow alpha (yet).
So, I made al little functions that buils the rgb string. I don't need to deal with transparencies for now.
我使用 rgbapng 指南针插件
安装
用法
编译为:
I use the rgbapng compass plugin
Install
Usage
Compiles to:
还有 ie-hex-str() 用于 IE 的 ##AARRGGBB 格式:
There's also ie-hex-str() for IE's ##AARRGGBB format:
来自文档:
From the documentation:
SASS
scale-color()
将以灵活的方式执行此操作:例如color: #{scale-color( $primary, $alpha: -50% )};
。完整参考此处。请注意,如果初始颜色(本例中的
$primary
)是纯色(没有透明度),则$alpha
值必须是负< /strong> 值(最高-100%
)以增加透明度。SASS
scale-color()
will do this in a flexible way: e.g.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.就我而言,rgba 不允许十六进制。
因此,我使用 transparentize 来减少 Alpha 通道。
In my case, rgba doesn't allow hex.
So, I use a transparentize which decreases the alpha channel.