Objective-c:如何创建不同深浅的颜色 [英] Objective-c : How to create different shades of a color
问题描述
是否有任何算法可以为给定的十六进制或 RGB 值创建不同的阴影?我曾尝试使用 alpha 增加 &减少,但在浅色(例如:白色)下看起来不太好.
Is there any algorithm that will create different shades for given Hex or RGB value? I have tried with alpha increasing & decreasing but it's not looking good with light colors(ex: white color).
推荐答案
最好的方法是使用 HSB 色彩空间.
The best way to do this is using HSB colour space.
这就是我在我最新的应用程序中使用键盘左手部分的方式...
It's how I did the left hand section of the keyboard in my latest app...
颜色的阴影"是H
值和S
值.然后您可以通过调整 B
值来更改它的亮度.
The "shade" of the colour is the H
value and S
value. You can then change the brightness of it by adjusting the B
value.
所以如果你有一个像......的颜色
So if you had a colour like...
HSB - 0.5, 0.9, 0.9 - 非常浅的蓝色
您可以通过更改 B 值来创建更深的阴影...
You could create darker shades by changing the B value...
HSB - 0.5, 0.9, 0.3 - 相同色调的深蓝色.
HSB - 0.5, 0.9, 0.3 - dark blue of the same shade.
创建 HSB 颜色,例如...
Create HSB colours like...
UIColor *color = [UIColor colorWithHue:0.5 saturation:0.9 brightness:0.4 alpha:1.0];
在我的应用中,我有一个最亮的亮度"和一个最暗的亮度".
In my app I had a "lightest brightness" and a "darkest brightness".
我通过将它们之间的差异除以部分的数量来计算出每个部分的亮度变化.
The I worked out the change in brightness for each section by dividing the difference between them by the number of sections.
使用 for
循环,我可以轻松创建不同的部分颜色.
Using a for
loop I was able to easily create the different section colours.
这篇关于Objective-c:如何创建不同深浅的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!