C# 十六进制代码阴影渐变生成器
我看过这里,但我不太明白是什么在那里进行。我对 C# 很陌生,我正在尝试制作一个十六进制代码梯度生成器。我得到了两串十六进制代码以及一堆向下/向上的代码。
例如:
black: 000000 to grey: 666666
with a step of 10.
这将生成 10 个不同的十六进制代码,这些代码将从这两点构建一个渐变。我想知道我会如何解决这个问题,或者是否有人可以给我一个如何做到这一点的算法。
谢谢!
I've had a look at here but I don't quite understand whats going on there. I'm quite new to c# and I'm trying to make a HEX code gradient generator. I am given two strings of hex codes with a bunch of step down/ups.
for example:
black: 000000 to grey: 666666
with a step of 10.
This would generate 10 different HEX codes that would build me a gradient from these two points. I was wondering how I'd go about this, or if someone could give me an algorithm for how to do this.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单梯度将范围线性划分为相等的部分。
例如,如果我们只看红色部分,并且希望分 5 步从 0 到 250(十进制),那么每个部分将为 ((250 - 0) / (5 - 1)) = 62.5。
这些值将为:0、62.5、125、187.5 和 250。
您现在要做的就是对每个组件重复此线性除法。
关于 Alpha 通道
请注意,
Color
数据结构包含一个 Alpha 通道值,该值不会反映在 0x666666 的情况中。如果您考虑模板的值 0xRRGGBB 并且假设 alpha 通道将为 0xFF(即完全不透明),那么一切都很好。但是,如果您想在渐变中包含 alpha 通道(如此处所有示例所示),那么对于模板 0xAARRGGBB,在您的情况下,alpha 通道将为 0,这意味着颜色将完全透明(不可见) )。
从 uint 构造
Color
关于
Color.FromArgb()
的一件烦人的事情是它接受int
值而不是uint
。因此,例如,向其输入 0xFF666666 将引发异常。为了克服这个问题,我们需要进行一些位移:
编辑:
GetColorFromArgb
解释要回答“如何将十六进制代码转换为RGB?”的问题,那么
GetColorFromArgb
已经将十六进制数转换为 R、G、B,而且还添加了 alpha 通道信息。GetColorFromArgb
的输入是一个 32 位无符号整数。如果我们看一下它的十六进制表示,那么它的模板是:0xAARRBBGG。GetColorFromArgb
使用位掩码和位移位逐个提取组件,然后调用Color.FromArgb(int a, int r, int g, int b)
。下面是
GetColorFromArgb
的更详细版本:如果问题是如何忽略 alpha 通道,那么您可以创建一个不提取 alpha 的
GetColorFromRGB()
方法信息,而不是传递固定值:int
而不是uint
输入。Simple gradients perform a linear division of a range into equal sections.
For example, if we look just at the red component and we'd like to go from 0 to 250 (decimal) in 5 steps, then each section will be ((250 - 0) / (5 - 1)) = 62.5.
The values will then be: 0, 62.5, 125, 187.5 and 250.
All you have to do now is to repeat this linear division for each component.
About the alpha channel
Note that the
Color
data structure contains an alpha-channel value, which is not reflected in the case of your 0x666666. If you consider your value of template 0xRRGGBB and you assume that the alpha channel will be 0xFF (i.e. totally opaque), then all is well.But if you'd like to include the alpha channel in your gradient (as seen in all examples here), then for the template 0xAARRGGBB, in your case the alpha channel will be 0, which means that the color will be totally transparent (invisible).
Contructing
Color
from uintOne annoying thing about
Color.FromArgb()
is that it accepts anint
value instead ofuint
. Thus, feeding it with 0xFF666666 for example will throw and exception.To overcome this, we need to dirty our hands with some bit shifting:
EDIT:
GetColorFromArgb
ExplainedTo answer the question "how would you convert a hex code into RGB?", then
GetColorFromArgb
already converts a hex number to R,G,B, but also adds the alpha-channel information.The input for
GetColorFromArgb
is a 32-bit unsigned integer. If we look at its hex representation, then its template is: 0xAARRBBGG.GetColorFromArgb
extracts the components one after the other by using bit-masks and bit-shifts, and then callingColor.FromArgb(int a, int r, int g, int b)
.Here's a more elaborated version of
GetColorFromArgb
:If the question was how to disregard the alpha-channel, then you can create a
GetColorFromRGB()
method that doesn't extract the alpha information and instead passes a fixed value:int
instead of auint
input.一个非常简单的方法是计算中间的红色、绿色和蓝色分量值......类似于:
A very simple method would be to calculate the intermediate Red, Green, and Blue component values... something like: