Sass/Compass - 将十六进制、RGB 或命名颜色转换为 RGBA

发布于 2025-01-05 09:58:55 字数 332 浏览 1 评论 0原文

这可能是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

七婞 2025-01-12 09:58:55

使用 Sass 中内置的 rgba 函数

设置颜色的不透明度。

示例:

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

参数:
(颜色)颜色
(数字)alpha — 0 到 1 之间的数字

返回:
(颜色)

代码:

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

Use the rgba function built into Sass

Sets the opacity of a color.

Examples:

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

Parameters:
(Color) color
(Number) alpha — A number between 0 and 1

Returns:
(Color)

Code:

rgba(#ff0000, 0.5); // Output is rgba(255,0,0,0.5);
梦明 2025-01-12 09:58:55

rgba 函数不适用于没有透明度的颜色,它再次返回十六进制。毕竟,这并不是要将十六进制转换为 rgba,我们只是从不允许 alpha 的十六进制中获利。

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

所以,我创建了一些构建 RGB 字符串的小函数。我现在不需要处理透明胶片。

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

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

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

So, I made al little functions that buils the rgb string. I don't need to deal with transparencies for now.

@function toRGB ($color) {
    @return "rgb(" + red($color) + ", " + green($color) + ", " + blue($color)+ ")";
}
苏辞 2025-01-12 09:58:55

我使用 rgbapng 指南针插件

rgbapng 是一个 Compass 插件,用于提供跨浏览器* 兼容
RGBA 支持。它的工作原理是创建单像素 alpha 透明 PNG
不支持 RGBA 的浏览器动态。它使用纯Ruby
ChunkyPNG 库可实现无忧安装和
部署。

安装

gem install compass-rgbapng

用法

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

编译为:

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

I use the rgbapng compass plugin

rgbapng is a Compass plugin for providing cross-browser* compatible
RGBA support. It works by creating single pixel alpha-transparent PNGs
on the fly for browsers that don't support RGBA. It uses the pure Ruby
ChunkyPNG library resulting in hassle-free installation and
deployment.

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 */

There's also ie-hex-str() for IE's ##AARRGGBB format:

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#{ie-hex-str(#fdfdfd)}', endColorstr='#{ie-hex-str(#f6f6f6)}',GradientType=0); /* IE6-9 */
宁愿没拥抱 2025-01-12 09:58:55
from_hex(hex_string, alpha = nil);

来自文档

从有效的 CSS 十六进制字符串创建新颜色。领先的哈希是
可选。

from_hex(hex_string, alpha = nil);

From the documentation:

Create a new color from a valid CSS hex string. The leading hash is
optional.

长发绾君心 2025-01-12 09:58:55

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.

薄暮涼年 2025-01-12 09:58:55

就我而言,rgba 不允许十六进制。

因此,我使用 transparentize 来减少 Alpha 通道。

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

In my case, rgba doesn't allow hex.

So, I use a transparentize which decreases the alpha channel.

.blue-background {
  background: transparentize(var.$color-my-blue, .8);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文