C# 十六进制代码阴影渐变生成器

发布于 2025-01-08 10:14:32 字数 359 浏览 1 评论 0原文

我看过这里,但我不太明白是什么在那里进行。我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2025-01-15 10:14:32

简单梯度将范围线性划分为相等的部分。

例如,如果我们只看红色部分,并且希望分 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 将引发异常。

为了克服这个问题,我们需要进行一些位移:

private static Color GetColorFromArgb( uint colorValue )
{
    Color color = Color.FromArgb(
                        (int)((colorValue & 0xFF000000) >> 24),
                        (int)((colorValue & 0x00FF0000) >> 16),
                        (int)((colorValue & 0x0000FF00) >> 8),
                        (int)((colorValue & 0x000000FF))
                    );

    return color;
}

编辑:GetColorFromArgb解释

要回答“如何将十六进制代码转换为RGB?”的问题,那么 GetColorFromArgb 已经将十六进制数转换为 R、G、B,而且还添加了 alpha 通道信息。

GetColorFromArgb 的输入是一个 32 位无符号整数。如果我们看一下它的十六进制表示,那么它的模板是:0xAARRBBGG。

GetColorFromArgb 使用位掩码和位移位逐个提取组件,然后调用 Color.FromArgb(int a, int r, int g, int b)

下面是 GetColorFromArgb 的更详细版本:

private static Color GetColorFromArgb(uint colorValue)
{
    int a = (int)((colorValue & 0xFF000000) >> 24);
    int r = (int)((colorValue & 0x00FF0000) >> 16);
    int g = (int)((colorValue & 0x0000FF00) >> 8);
    int b = (int)(colorValue & 0x000000FF);

    Color color = Color.FromArgb(a, r, g, b);

    return color;
}

如果问题是如何忽略 alpha 通道,那么您可以创建一个不提取 alpha 的 GetColorFromRGB() 方法信息,而不是传递固定值:

private static Color GetColorFromRGB(int colorValue)
{
    int a = 0xFF; // Fully opaque
    int r = (int)((colorValue & 0x00FF0000) >> 16);
    int g = (int)((colorValue & 0x0000FF00) >> 8);
    int b = (int)(colorValue & 0x000000FF);

    Color color = Color.FromArgb(a, r, g, b);

    return color;
}
  • 请注意,由于您使用 0xRRGGBB 值,因此可以使用 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 uint

One annoying thing about Color.FromArgb() is that it accepts an int value instead of uint. Thus, feeding it with 0xFF666666 for example will throw and exception.

To overcome this, we need to dirty our hands with some bit shifting:

private static Color GetColorFromArgb( uint colorValue )
{
    Color color = Color.FromArgb(
                        (int)((colorValue & 0xFF000000) >> 24),
                        (int)((colorValue & 0x00FF0000) >> 16),
                        (int)((colorValue & 0x0000FF00) >> 8),
                        (int)((colorValue & 0x000000FF))
                    );

    return color;
}

EDIT: GetColorFromArgb Explained

To 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 calling Color.FromArgb(int a, int r, int g, int b).

Here's a more elaborated version of GetColorFromArgb:

private static Color GetColorFromArgb(uint colorValue)
{
    int a = (int)((colorValue & 0xFF000000) >> 24);
    int r = (int)((colorValue & 0x00FF0000) >> 16);
    int g = (int)((colorValue & 0x0000FF00) >> 8);
    int b = (int)(colorValue & 0x000000FF);

    Color color = Color.FromArgb(a, r, g, b);

    return color;
}

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:

private static Color GetColorFromRGB(int colorValue)
{
    int a = 0xFF; // Fully opaque
    int r = (int)((colorValue & 0x00FF0000) >> 16);
    int g = (int)((colorValue & 0x0000FF00) >> 8);
    int b = (int)(colorValue & 0x000000FF);

    Color color = Color.FromArgb(a, r, g, b);

    return color;
}
  • Note that since you use 0xRRGGBB values, you can use an int instead of a uint input.
冰魂雪魄 2025-01-15 10:14:32

一个非常简单的方法是计算中间的红色、绿色和蓝色分量值......类似于:

Dim tOriginHexColor As String = "000000"
Dim tTargetHexColor As String = "666666"
Dim tSteps As Byte = 10

Dim tOriginColor as Color = ColorTranslator.FromHtml(tOriginHexColor)
Dim tTargetColor As Color = ColorTranslator.FromHtml(tTargetHexColor)

Dim tRedStep As Byte = tTargetColor.R - tOriginColor.R
Dim tBlueStep As Byte = tTargetColor.R - tOriginColor.B
Dim tGreenStep As Byte = tTargetColor.R - tOriginColor.G

For tIndex As Int16 = 0 To tSteps - 1
  Debug.Print(ColorTranslator.ToHtml(Color.FromArgb(tOriginColor.R + tRedStep, tOriginColor.G + tGreenStep, tOriginColor.B + tBlueStep)))
Next

A very simple method would be to calculate the intermediate Red, Green, and Blue component values... something like:

Dim tOriginHexColor As String = "000000"
Dim tTargetHexColor As String = "666666"
Dim tSteps As Byte = 10

Dim tOriginColor as Color = ColorTranslator.FromHtml(tOriginHexColor)
Dim tTargetColor As Color = ColorTranslator.FromHtml(tTargetHexColor)

Dim tRedStep As Byte = tTargetColor.R - tOriginColor.R
Dim tBlueStep As Byte = tTargetColor.R - tOriginColor.B
Dim tGreenStep As Byte = tTargetColor.R - tOriginColor.G

For tIndex As Int16 = 0 To tSteps - 1
  Debug.Print(ColorTranslator.ToHtml(Color.FromArgb(tOriginColor.R + tRedStep, tOriginColor.G + tGreenStep, tOriginColor.B + tBlueStep)))
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文