SCSS/SASS @each 循环使用 HEX 和 RGB 格式的调色板
SCSS/SASS @each loop Color Palette with HEX and RGB formats
如何使用 @each 循环
显示此 HEX 和 RGB 格式的调色板
调色板变量:
$colors: (
"transparent": transparent,
"current": currentColor,
"white": (
hex: #FFFFFF,
rgb: (255, 255, 255)
),
"black": (
hex: #000000,
rgb: (0, 0, 0)
)
) !default;
编译成CSS后的预期结果:
.color-transparent {
color: transparent;
}
.color-current {
color: currentColor;
}
.color-white {
--color-opacity: 1;
color: #FFFFFF;
color: rgba(255, 255, 255, var(--color-opacity));
}
.color-black {
--color-opacity: 1;
color: #000000;
color: rgba(0, 0, 0, var(--color-opacity));
}
以下是使用 @each
和地图的方法。
$colors: (
"transparent": transparent,
"current": currentColor,
"white": (
hex: #FFFFFF,
rgba: (255, 255, 255, var(--color-opacity))
),
"black": (
hex: #000000,
rgba: (0, 0, 0, var(--color-opacity))
)
) !default;
// $COLOR = KEY, $VALUE = VALUE, $COLORS = MAP
@each $color, $value in $colors {
// SINGLE VALUE
@if $color == "transparent" {
.color-#{$color} {
color: #{$value};
}
} @else if $color == "current" {
.color-#{$color} {
color: $value;
}
// SUBMAP - SINCE $VALUE IS ANOTHER MAP HERE.
} @else {
// SET THE COLOR NAME
.color-#{$color} {
// SET YOUR COLOR OPACITY
--color-opacity: 1;
// LOOP THROUGH THE TWO COLORS IN THE SUBMAP
@each $color_name, $value_c in $value {
@if $color_name == hex {
color: $value_c;
} @else {
color: rbga( $value_c );
}
}
}
}
}
CSS 输出
.color-transparent {
color: transparent;
}
.color-current {
color: currentColor;
}
.color-white {
--color-opacity: 1;
color: #FFFFFF;
color: rbga(255, 255, 255, var(--color-opacity));
}
.color-black {
--color-opacity: 1;
color: #000000;
color: rbga(0, 0, 0, var(--color-opacity));
}
编辑 RGBA() 函数问题
要解决 rgba()
抛出频道错误的问题,您可以删除 hex
部分之后的 @else
并将十六进制值传递到 rgba()
功能。这会将十六进制转换为 rgb 值。
$colors: (
"transparent": transparent,
"current": currentColor,
"white": (
hex: #FFFFFF,
rgba: (255, 255, 255, var(--color-opacity))
),
"black": (
hex: #000000,
rgba: (0, 0, 0, var(--color-opacity))
)
) !default;
// $COLOR = KEY, $VALUE = VALUE, $COLORS = MAP
@each $color, $value in $colors {
// SINGLE VALUE
@if $color == "transparent" {
.color-#{$color} {
color: #{$value};
}
} @else if $color == "current" {
.color-#{$color} {
color: $value;
}
// SUBMAP - SINCE $VALUE IS ANOTHER MAP HERE.
} @else {
// SET THE COLOR NAME
.color-#{$color} {
// SET YOUR COLOR OPACITY
--color-opacity: 1;
// LOOP THROUGH THE TWO COLORS IN THE SUBMAP
@each $color_name, $value_c in $value {
// WE ONLY WANT TO USE HEX, BECAUSE RGBA() IS A NATIVE FUNCTION
@if $color_name == hex {
color: $value_c;
color: rgba( $value_c, var(--color-opacity));
}
}
}
}
}
如何使用 @each 循环
显示此 HEX 和 RGB 格式的调色板调色板变量:
$colors: (
"transparent": transparent,
"current": currentColor,
"white": (
hex: #FFFFFF,
rgb: (255, 255, 255)
),
"black": (
hex: #000000,
rgb: (0, 0, 0)
)
) !default;
编译成CSS后的预期结果:
.color-transparent {
color: transparent;
}
.color-current {
color: currentColor;
}
.color-white {
--color-opacity: 1;
color: #FFFFFF;
color: rgba(255, 255, 255, var(--color-opacity));
}
.color-black {
--color-opacity: 1;
color: #000000;
color: rgba(0, 0, 0, var(--color-opacity));
}
以下是使用 @each
和地图的方法。
$colors: (
"transparent": transparent,
"current": currentColor,
"white": (
hex: #FFFFFF,
rgba: (255, 255, 255, var(--color-opacity))
),
"black": (
hex: #000000,
rgba: (0, 0, 0, var(--color-opacity))
)
) !default;
// $COLOR = KEY, $VALUE = VALUE, $COLORS = MAP
@each $color, $value in $colors {
// SINGLE VALUE
@if $color == "transparent" {
.color-#{$color} {
color: #{$value};
}
} @else if $color == "current" {
.color-#{$color} {
color: $value;
}
// SUBMAP - SINCE $VALUE IS ANOTHER MAP HERE.
} @else {
// SET THE COLOR NAME
.color-#{$color} {
// SET YOUR COLOR OPACITY
--color-opacity: 1;
// LOOP THROUGH THE TWO COLORS IN THE SUBMAP
@each $color_name, $value_c in $value {
@if $color_name == hex {
color: $value_c;
} @else {
color: rbga( $value_c );
}
}
}
}
}
CSS 输出
.color-transparent {
color: transparent;
}
.color-current {
color: currentColor;
}
.color-white {
--color-opacity: 1;
color: #FFFFFF;
color: rbga(255, 255, 255, var(--color-opacity));
}
.color-black {
--color-opacity: 1;
color: #000000;
color: rbga(0, 0, 0, var(--color-opacity));
}
编辑 RGBA() 函数问题
要解决 rgba()
抛出频道错误的问题,您可以删除 hex
部分之后的 @else
并将十六进制值传递到 rgba()
功能。这会将十六进制转换为 rgb 值。
$colors: (
"transparent": transparent,
"current": currentColor,
"white": (
hex: #FFFFFF,
rgba: (255, 255, 255, var(--color-opacity))
),
"black": (
hex: #000000,
rgba: (0, 0, 0, var(--color-opacity))
)
) !default;
// $COLOR = KEY, $VALUE = VALUE, $COLORS = MAP
@each $color, $value in $colors {
// SINGLE VALUE
@if $color == "transparent" {
.color-#{$color} {
color: #{$value};
}
} @else if $color == "current" {
.color-#{$color} {
color: $value;
}
// SUBMAP - SINCE $VALUE IS ANOTHER MAP HERE.
} @else {
// SET THE COLOR NAME
.color-#{$color} {
// SET YOUR COLOR OPACITY
--color-opacity: 1;
// LOOP THROUGH THE TWO COLORS IN THE SUBMAP
@each $color_name, $value_c in $value {
// WE ONLY WANT TO USE HEX, BECAUSE RGBA() IS A NATIVE FUNCTION
@if $color_name == hex {
color: $value_c;
color: rgba( $value_c, var(--color-opacity));
}
}
}
}
}