TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

CSS Gradients that avoid the “gray dead zone”

1075 点作者 joshwcomeau超过 3 年前

47 条评论

rikroots超过 3 年前
Just before Christmas I got an urge to add a dither filter to my JS canvas library. The past few weeks have certainly been a very intense adventure through a very deep rabbit hole involving color spaces, color distances and discovering how to generate a best-fit reduced palette of colors and then dither it (hello, blue noise!)<p>I finally managed to complete the work last week and I&#x27;m quite happy with the results. Using the library people can set the colors of graphical objects or gradients using any CSS color space string (RGB, HSL, HWB, LAB, LCH) and the color should render as expected. The reduce-noise filter uses LAB internally to calculate color distances, but in the end I compromised and used euler distances for the algorithm rather than one of the more modern algorithms.<p>Work is still on a branch in the library[1] but I&#x27;m hoping to merge it into the main code in the next week or so (after testing across devices&#x2F;screens&#x2F;etc).<p>Aside: Safari has gone ahead and introduced support for HWB, LAB and LCH color strings in CSS. Sadly, they&#x27;re not recognised by Safari&#x27;s canvas engine (yet). Also, their HSL and HWB gradients seem to be the same as the RGB gradients; their LAB&#x2F;LCH gradients seem to dodge the dreaded Gray Zone.<p>[1] - <a href="https:&#x2F;&#x2F;github.com&#x2F;KaliedaRik&#x2F;Scrawl-canvas&#x2F;tree&#x2F;dec-21-dither-filter" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;KaliedaRik&#x2F;Scrawl-canvas&#x2F;tree&#x2F;dec-21-dith...</a>
评论 #29897627 未加载
评论 #29897717 未加载
评论 #29954145 未加载
评论 #29903308 未加载
评论 #29902779 未加载
jrowen超过 3 年前
This article seems to give RGB a bad rap but I think it makes the most sense for gradients. I wouldn&#x27;t want a yellow to blue gradient to cycle through a (partial) rainbow of colors. But I also wouldn&#x27;t really ever choose to make a pure yellow to pure blue gradient. Most pleasing gradients you see in the wild use colors that are more similar to begin with, so it&#x27;s a bit of a strawman. It&#x27;s really just about carefully choosing your start and end (and optionally intermediate) colors.
评论 #29898736 未加载
评论 #29903463 未加载
评论 #29899994 未加载
评论 #29899194 未加载
评论 #29902352 未加载
评论 #29899805 未加载
评论 #29901374 未加载
评论 #29903600 未加载
评论 #29899189 未加载
circlingthesun超过 3 年前
There is a related video by minute physics about how the RGB color space stores the square root of the brightness of each color channel, because human color perception is roughly logarithmic. Bad color blending is therefore pretty commonplace because it uses the average of square roots instead of the square root of the average of the original brightness values. See: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LKnqECcg6Gw" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LKnqECcg6Gw</a>
评论 #29907657 未加载
评论 #29904693 未加载
liuliu超过 3 年前
Because Hue is a degree, you can walk either clockwise or counter-clockwise. That is why in the Javascript widget (the one to select midpoints and color spaces), HSL v.s. Lab v.s. HCL acted quite differently. You probably also need a tweak for that in the &quot;Gradient Generator&quot; widget too.
评论 #29896147 未加载
评论 #29897110 未加载
hammock超过 3 年前
What is the LAB color space that is provided in the tool but not discussed in the article? For all the hubbub about HSL and HCL, LAB looks the best to me.
评论 #29898843 未加载
评论 #29897982 未加载
评论 #29898424 未加载
评论 #29898034 未加载
评论 #29905250 未加载
iLoveOncall超过 3 年前
I find the article extremely dishonest as it goes from using a &quot;yellow to blue gradient&quot; (let&#x27;s call that the initial requirement) and turns it into a supposedly better &quot;yellow to orange to red to pink to purple to blue&quot; gradient.<p>I think anybody that has to choose between the &quot;gray dead zone&quot; and the &quot;rainbow&quot; will ALWAYS settle for the &quot;gray dead zone&quot; if the initial requirement is to make a &quot;yellow to blue gradient&quot;.<p>Does it contain valuable information? Yes. Does it avoid the &quot;gray dead zone&quot; problem? Not at all, unless you claim that bringing another problem magnitudes larger is &quot;avoiding the problem&quot;.
评论 #29905202 未加载
smagoun超过 3 年前
This fantastic article proposes new colorspaces (okhsl, oklab) for color pickers and is worth the read for anyone interested in color: <a href="https:&#x2F;&#x2F;bottosson.github.io&#x2F;posts&#x2F;colorpicker&#x2F;" rel="nofollow">https:&#x2F;&#x2F;bottosson.github.io&#x2F;posts&#x2F;colorpicker&#x2F;</a><p>Many similar problems to solve as in the gradient generator, but approached from a different point of view, and with some novel ideas.
评论 #29901377 未加载
dexwiz超过 3 年前
Love the article. Small UX gripe: it took me a while to figure out how to use the gradient sliders. The box at the top looks like a handle, and I was trying to interact with that. But it looks like I have to click the bar to move the box, but there is no visual indicator that the bar itself is a point of interaction other than a cursor change (which is really subtle).
评论 #29898141 未加载
评论 #29896333 未加载
评论 #29898468 未加载
评论 #29897562 未加载
eatonphil超过 3 年前
The gradient generator Josh introduces here is pretty nice [0]! And the HSL vs RGB theory is also pretty interesting. I&#x27;ll give this generator a try next time I want one.<p>But my favorite gradient generator at the moment (most pleasant gradients IMO) is still [1].<p>[0] <a href="https:&#x2F;&#x2F;www.joshwcomeau.com&#x2F;gradient-generator&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.joshwcomeau.com&#x2F;gradient-generator&#x2F;</a><p>[1] <a href="https:&#x2F;&#x2F;mycolor.space&#x2F;gradient" rel="nofollow">https:&#x2F;&#x2F;mycolor.space&#x2F;gradient</a>
评论 #29896432 未加载
NelsonMinar超过 3 年前
As the article says, the core problem is CSS interpolates in RGB colorspace. CSS Color 4 seems to have adapted a color space parameter for interpolation in its drafts. See discussion here: <a href="https:&#x2F;&#x2F;github.com&#x2F;w3c&#x2F;csswg-drafts&#x2F;issues&#x2F;5833" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;w3c&#x2F;csswg-drafts&#x2F;issues&#x2F;5833</a>
评论 #29906878 未加载
评论 #29903911 未加载
bugeats超过 3 年前
&gt; Now, HSL isn&#x27;t necessarily the best color mode to use in every situation; it tends to produce gradients that can be overly bright and vivid, because it doesn&#x27;t take into account human perception.<p>Shout-out to to HSLUV which does exactly that.<p><a href="https:&#x2F;&#x2F;www.hsluv.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.hsluv.org&#x2F;</a>
评论 #29900418 未加载
Trufa超过 3 年前
As a (web&#x2F;app) developer, I&#x27;d really wish I had any sort of talent do beautiful things, this person clearly has a sense of aesthetics that I don&#x27;t, while I know that practice would make it better, I just don&#x27;t feel that I&#x27;d get there.<p>Congrats to the author for such a cool product!
评论 #29896614 未加载
评论 #29895995 未加载
评论 #29898838 未加载
tvanantwerp超过 3 年前
I used to do gradient interpolations all the time in dataviz using D3, and using HSL&#x2F;HCL&#x2F;LAB always created better gradients. I wish we had color mode options in CSS gradients, but this is a good workaround for now.
评论 #29896541 未加载
Retr0id超过 3 年前
I made a less-polished version of this too, although mine uses the Oklab colourspace for interpolation:<p><a href="https:&#x2F;&#x2F;www.da.vidbuchanan.co.uk&#x2F;widgets&#x2F;css-gradient-fixer&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.da.vidbuchanan.co.uk&#x2F;widgets&#x2F;css-gradient-fixer&#x2F;</a>
评论 #29898578 未加载
monokai_nl超过 3 年前
As part of my logo generator I&#x27;ve spent quite some time to create a color algorithm that takes one color and create a gradient based on an intensity slider and a &quot;mode&quot;. It proved quite useful, so I&#x27;ve implemented it as a free stand alone tool for web developers to quickly create aesthetically pleasing CSS gradients:<p><a href="https:&#x2F;&#x2F;mybrandnewlogo.com&#x2F;color-gradient-generator" rel="nofollow">https:&#x2F;&#x2F;mybrandnewlogo.com&#x2F;color-gradient-generator</a>
bborud超过 3 年前
This is one of those things that has been at the edge of my attention for years, and when someone articulates and demonstrates it, I had an &quot;aha! of course&quot; reaction. :)
leni536超过 3 年前
The darkish gray dip is mostly avoided by using a linear colorspace. Interpolating in HSV can introduce weird artifacts. Mostly due to H being circular, if you naively interpolate between two close shade of red, you might go through all of the colors of the rainbow.<p>Interpolaring in a perceptual colorspace might make sense in certain applications, but if arithmetic on color values is involved then a linear colorspace is a good default (and premultiplied alpha, but that&#x27;s an other story).
peterisza超过 3 年前
Related video: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LKnqECcg6Gw" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LKnqECcg6Gw</a>
评论 #29901526 未加载
ketzo超过 3 年前
Always psyched to see a post from josh.<p>Really insightful deep dives into a <i>very</i> misunderstood technology, presented with a fun, lighthearted writing style that I find myself trying to emulate in my own technical writing.
jrochkind1超过 3 年前
This discussion thread already has an unusual number of posts specifically praising the author by name. It&#x27;s very weird and unusual on HN. But he&#x27;s just a really popular guy?
alberth超过 3 年前
For those who didn&#x27;t realize, there&#x27;s a ton of interactive sliders on that page.<p>(it didn&#x27;t dawn on me until I accidentally bumped a slider and realized the page was interactive)
评论 #29896447 未加载
评论 #29896593 未加载
kazinator超过 3 年前
There are more colors spaces than just HSV and RGB.<p>The HSV gradients look wonky; the HSV space was designed for easy color picking UI with efficient calculations on 1980&#x27;s hardware, not for interpolation.<p>I found this site where you can play with interpolation via various color spaces:<p><a href="https:&#x2F;&#x2F;colordesigner.io&#x2F;gradient-generator" rel="nofollow">https:&#x2F;&#x2F;colordesigner.io&#x2F;gradient-generator</a><p>LAB looks good; but interestingly, going from yellow to blue, it doesn&#x27;t interpolate through green at all. Which makes sense, since yellow-blue is one axis in LAB, whereas red-green is another.
scotty79超过 3 年前
Is noone bothered that proposed gradient alternatives go through additional completely different colors?
评论 #29901105 未加载
评论 #29903664 未加载
rco8786超过 3 年前
Every once in a while you come across something on the internet that makes you stop and go &quot;whoa, that is cool&quot;. This is one of those for me.
ridaj超过 3 年前
If we&#x27;re going to think about gradients at that level, we should also think about perceptual nonlinearities that create all kinds of distracting visual artifacts... See e.g. <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20754364" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20754364</a>
gnabgib超过 3 年前
Since this is posted by you, Josh.. is the date in the article inaccurate (should the year be 2022?) or did you just decided to push to HN on the one year anniversary? (Title might benefit from the year)
评论 #29895875 未加载
eternityforest超过 3 年前
The problem with HSL gradients is that you now have a completely different color, and the whole thing looks like nothing found in nature. Unless the rainbow is an intentional design element, it just doesn&#x27;t quite look right.<p>It&#x27;s useful, but not a substitute for really carefully picking your colors so it all works.<p>One of the harder things in creative projects is when you have two individual elements you want to use, but you don&#x27;t like what happens if you add both.
azangru超过 3 年前
This was discussed on the http203 podcast: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;cGyLHxn16pE" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;cGyLHxn16pE</a>
AtNightWeCode超过 3 年前
Nice. Things ending up being gray is a common problem on the web.<p>I took a deep dive into gradients some time ago. Not CSS but custom calculations that evened out things like brightness, luminescence, contrast and so on. My key takeaway was that my preferred way of doing gradients was to use similar hues and also play around with the saturation of the colors. One does not need complimentary colors to get that gradient feel.
kristopolous超过 3 年前
I might be missing something but what happens when you do it with the already supported HSL (for well over a decade)<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;CSS&#x2F;color_value&#x2F;hsl%28%29" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;CSS&#x2F;color_value...</a><p>I&#x27;m in mobile. sorry for not investigating this myself
theandrewbailey超过 3 年前
It would be convenient if gradient calculations would be hinted by the color space used to define the start&#x2F;end colors.
r2222超过 3 年前
You can create multi-stop gradients easily with this tool, but doing so with pure blue and yellow looks nothing like the blend of those two colors would look. I think you should just pick colors that fit better together, unless the rainbow effect of walking along the hue angle is the effect you’re going for.
mckoss超过 3 年前
Really good videos on color spaces and gradients from Chrome Dev:<p><a href="https:&#x2F;&#x2F;youtu.be&#x2F;cGyLHxn16pE" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;cGyLHxn16pE</a> <a href="https:&#x2F;&#x2F;youtu.be&#x2F;Uh95jZPTDfw" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;Uh95jZPTDfw</a>
stu_k超过 3 年前
This also addresses an issue when you transition from black to transparent to emulate a shadow, and end up with a &quot;cliff&quot; where the gradient abruptly changes. You can see this in the tool when you transition from black to white with LRGB. Any of the other color spaces avoid it. Very cool!
toastal超过 3 年前
Personally I&#x27;ve used SVGs and linear gradients to achieve a smooth gradient and blurs. I made a CodePen about blurs several year ago: <a href="https:&#x2F;&#x2F;codepen.io&#x2F;toastal&#x2F;pen&#x2F;NAVybq" rel="nofollow">https:&#x2F;&#x2F;codepen.io&#x2F;toastal&#x2F;pen&#x2F;NAVybq</a>
jtth超过 3 年前
Why are there two popovers in a row when paging through the page?
评论 #29896817 未加载
评论 #29901752 未加载
Jerry2超过 3 年前
So, the &quot;solution&quot; for having a gradient go from yellow to a blue (via a gray region) is to have yellow go through green and then blue? Color me unimpressed.<p>I remember years ago using non-linear functions to avoid a similar problem in a different field. I bet the same solution would work here and wouldn&#x27;t look so &quot;wrong&quot;. You could pick some non-linear fun to interpolate between values. And you could interpolate different RGB channels at an offset. The reult would be less gray in the middle.
评论 #29901106 未加载
评论 #29901061 未加载
mfbx9da4超过 3 年前
Wow! Subscribing to the newsletter was just such a delightful experience that I subscribed despite not wanting yet another newsletter.
darepublic超过 3 年前
Nice little pop up with that clay figure -- actually clicked &#x27;Yes&#x27; for once
lwansbrough超过 3 年前
lab looks best IMO. Safari supports lab in CSS, but I don&#x27;t have a way to test: do linear-gradients in Safari where lab is the colour space used for the two colours, does it do the gradient in lab colour space?
arendtio超过 3 年前
I enjoyed this one very much. Thanks for creating such high quality content.
arlburn超过 3 年前
A great read. I do hope that people will try to incorporate this technique.
adtac超过 3 年前
Is there a primer to colour spaces for a beginner?
评论 #29896894 未加载
jcun4128超过 3 年前
Ha that thing that moves on the side got me.
aaron_m04超过 3 年前
What gray zone?
PoopScript超过 3 年前
very cool
xibalba超过 3 年前
I really, really like Mr. Comeau&#x27;s &quot;Me&quot; avatar. In fact, his whole site is quite beautiful and pleasing.
评论 #29895750 未加载
评论 #29895799 未加载