English 中文(简体)
将LESS变亮和变暗转换为CSS颜色混合
原标题:Convert LESS lighten and darken to CSS color-mix

我正在尝试将一些LESS颜色函数(变亮/变暗)转换为使用CSS颜色混合函数,这样我就可以使用CSS变量。

我有一个在内部使用这些的依赖项,并编写了一个NPM安装后脚本,可以自动转换它们。转换工作正常,但我面临的问题是输出关闭。

以下是输出示例:

对于颜色混合,我使用:

color-mix(in srgb, white 5%, var(--my-color)); // lighten(@mycolor, 5%);
color-mix(in srgb, white 50%, var(--my-color)); // lighten(@mycolor, 50%);

你知道我如何才能更紧密地匹配我从LESS看到的东西吗?

问题回答

LESS<code>lighten()</code>处理HSL颜色值的亮度部分请参阅此处的文档。因此,如果您的起始颜色是#348298,转换为hsl(193,49%,40%),然后使用LESS将其减亮50%意味着50%将被添加到该颜色的亮度(最后)值中,因此生成的颜色为hsl。

您可以在CSS中复制这一点,只需使用HSL格式的颜色并调整亮度值。因此,可以使用CSS变量来处理色调和饱和度分量,但不能使用亮度。

颜色混合的效果截然不同,它将两种颜色按指定比例混合在一起。我相信你可以通过实验找到产生你想要的颜色的值,但我不知道如何从数学上做到这一点。

:root {
  --h1: 193;
  --s1: 49%;
}
body {
  display: flex;
  gap: 2px;
  font-family: sans-serif;
}
div {
  color: white;
  background-color: hsl(var(--h1), var(--s1), 40%); /* base colour */
  flex: 1;
  text-align: center;
  padding: 1em;
}
.m1 {
  background-color: hsl(var(--h1), var(--s1), 45%); /* five percent added to luminance */
}
.m2 {
  background-color: hsl(var(--h1), var(--s1), 90%); /* fifty percent added to luminance */
}
<div>Base</div>
<div class="m1">add 5% luminance</div>
<div class="m2">add 50% luminance</div>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签