重大变化:空 Alpha 通道
在 Dart Sass 1.64.3 之前,在 JS 和 Dart API 中,如果 null
传递给 SassColor
构造函数,它将被视为 1。现在已弃用。用户应明确传递 1 或 undefined
。
Sass 正在努力添加对 CSS 颜色模块级别 4 的支持。该模块的变化之一是 "缺少组件" 的思想:如果缺少像 alpha
这样的颜色分量,它通常被视为 0,但如果用另一种颜色进行插值(例如在渐变或动画中),它将自动采用另一种颜色的值。
¥Sass is working on adding support for the CSS Color Module Level 4. One of the
changes in this module is the idea of "missing components": if a color
component like alpha
is missing, it’s mostly treated as 0, but if it’s
interpolated with another color (such as in a gradient or an animation) it will
automatically take on the other color’s value.
我们需要一种方法让 JS 和 Dart API 的用户访问和设置缺失的通道,而 null
是最自然的方法。在大多数情况下,这不是问题;想要创建不透明颜色的调用者通常只是忽略 alpha
参数(或在 JS 中传递 undefined
)。但是,如果调用者显式传递 null
,则最终将被视为透明颜色而不是不透明颜色。
¥We need a way for users of the JS and Dart APIs to access and set missing
channels, and null
is the most natural way to do that. In most cases, this
isn’t an issue; callers who intend to create opaque colors usually just leave
out the alpha
parameter anyway (or pass undefined
in JS). But if callers are
explicitly passing null
, that will eventually be treated as a transparent
color instead of an opaque one.
要保留当前行为,你所需要做的就是在未设置 alpha
的情况下显式传递 1。在 JS 中:
¥To preserve the current behavior, all you need to do is explicitly pass 1 if
alpha
is unset. In JS:
new sass.SassColor({
red: 102,
green: 51,
blue: 153,
alpha: alpha ?? 1,
});
在 Dart 中:
¥And in Dart:
sass.SassColor.rgb(102, 51, 153, alpha ?? 1);
💡 Fun fact:
Sass API 的 TypeScript 类型已经禁止将 null
作为 alpha
传递;只允许缺席、undefined
或 Number
。但在 Dart Sass 1.64.3 之前,如果你没有使用 TypeScript 并且确实通过了 null
,它仍然会被视为不透明颜色。
¥The TypeScript types for the Sass API already forbid passing null
as
alpha
; it’s only allowed to be absent, undefined
, or a Number
. But prior
to Dart Sass 1.64.3, if you weren’t using TypeScript and you did pass null
it would still be treated as an opaque color.
过渡期过渡期 permalink
¥Transition Period
- Dart Sass
- since 1.64.3
- LibSass
- ✗
- Ruby Sass
- ✗
在 Dart Sass 1.64.3 和即将发布的 CSS 颜色级别 4 支持之间,Dart Sass 将继续将 null
alpha
值解释为不透明颜色。但是,它会发出弃用警告,以鼓励作者明确传递 alpha
1。
¥Between Dart Sass 1.64.3 and the upcoming release of support for CSS Colors
Level 4, Dart Sass will continue to interpret a null
alpha
value as an opaque
color. However, it will emit a deprecation warning to encourage authors to
explicitly pass alpha
1 instead.