基于文件加载百分比的 RGB 渐变

发布于 2024-08-18 18:39:12 字数 890 浏览 8 评论 0原文

我正在开发我的第一个 Flash 项目,对于我的预加载器,我想根据加载的百分比做一个非常简单的渐变。预加载器显示“77% 已加载...”,其中数字 77 是一个称为percentLoaded 的动态文本实例。我希望%Loaded 的textColor 以灰度形式从#000000 渐变到#FFFFFF。

因此,我不能简单地这样做:

percentLoaded.textColor=(currentValue/100)*0xFFFFFF;

这只是将 textColor 转换为 FFFFFF 的倍数,但输出颜色,因为它不是三个单独的组件。目前,这就是我所得到的:

percentLoaded.text=currentValue.toString();
percentLoaded.textColor=rgb2hex((currentValue/100)*255, (currentValue/100)*255, (currentValue/100)*255);

其中“rgb2hex”是在类中定义的函数,如下所示:

public function rgb2hex(r:Number, g:Number, b:Number) {
    return '0x'+(r << 16 | g << 8 | b).toString(16).toUpperCase();
}

但看起来这实际上并没有改变字体的颜色。我已导入 flash.text.TextField 和 flash.display.MovieClip,但不确定是否缺少其他内容。使用字符串连接会更容易吗?或者 currentValue/100 是否发生了一些事情并将其作为数字传递?

如果好奇,我找到了 rgb2hex 的代码

谢谢!

I'm working on my first Flash project, and for my preloader I'd like to do a really simple gradient based on the percentage loaded. The preloader says "77% loaded...", where the number 77 is a dynamic text instance called percentLoaded. I'd like the textColor of percentLoaded to change on a gradient from #000000 to #FFFFFF, in gray-scale.

Therefore, I can't simply do:

percentLoaded.textColor=(currentValue/100)*0xFFFFFF;

This just converts the textColor to a multiple of FFFFFF, but outputs a color since it's not three separate components. Currently, here's what I've got:

percentLoaded.text=currentValue.toString();
percentLoaded.textColor=rgb2hex((currentValue/100)*255, (currentValue/100)*255, (currentValue/100)*255);

Where "rgb2hex" is a function defined within the class as such:

public function rgb2hex(r:Number, g:Number, b:Number) {
    return '0x'+(r << 16 | g << 8 | b).toString(16).toUpperCase();
}

It doesn't look like this is actually changing the color of the font though. I've imported flash.text.TextField and flash.display.MovieClip, but am not sure if I'm missing something else. Would this be easier to do with string concatenation? Or is there maybe something going on with currentValue/100 and passing that as a Number?

If curious, I found the code for rgb2hex here.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

岛徒 2024-08-25 18:39:12
percentLoaded.textColor=(currentValue/100)*0xFFFFFF;

这已经很接近了。

要获得灰度,您确实需要拥有每个组件,但这仍然可以通过您的类似方法来完成:

percentLoaded.textColor = uint((currentValue / 100) * 0xFF) * 0x010101;
percentLoaded.textColor=(currentValue/100)*0xFFFFFF;

This is close.

To get a grayscale, you indeed need to have each component, but this can still be done by similar method of yours:

percentLoaded.textColor = uint((currentValue / 100) * 0xFF) * 0x010101;
不再让梦枯萎 2024-08-25 18:39:12

你很幸运,我最近在一个项目中碰巧做到了这一点,只是用了不同的颜色。这是一个函数,它将基于 0-1 的范围返回两个其他颜色之间的颜色值。我认为该代码非常不言自明,但如果您遇到任何问题,请告诉我。

private function getBetweenColourByPercent(value:Number = 0.5 /* 0-1 */, highColor:uint = 0xFFFFFF, lowColor:uint = 0x000000):uint {
    var r:uint = highColor >> 16;
    var g:uint = highColor >> 8 & 0xFF;
    var b:uint = highColor & 0xFF;

    r += ((lowColor >> 16) - r) * value;
    g += ((lowColor >> 8 & 0xFF) - g) * value;
    b += ((lowColor & 0xFF) - b) * value;

    return (r << 16 | g << 8 | b);
}

泰勒.

You're in luck, I happen to have done this very recently in a project, only with different colours. Here is a function that will return a colour value between two other colours, based on a range of 0-1. I think the code is pretty self explanatory, but let me know if you run into any issues.

private function getBetweenColourByPercent(value:Number = 0.5 /* 0-1 */, highColor:uint = 0xFFFFFF, lowColor:uint = 0x000000):uint {
    var r:uint = highColor >> 16;
    var g:uint = highColor >> 8 & 0xFF;
    var b:uint = highColor & 0xFF;

    r += ((lowColor >> 16) - r) * value;
    g += ((lowColor >> 8 & 0xFF) - g) * value;
    b += ((lowColor & 0xFF) - b) * value;

    return (r << 16 | g << 8 | b);
}

Tyler.

始终不够 2024-08-25 18:39:12
percentLoaded.textColor = int((currentValue / 100) * 0xFF) * 0x010101;

乘以 0x010101 之前必须强制转换为 int。 8 位颜色值只能是 0-255 之间的整数。如果不在这些范围内,乘以 0x010101 可能会导致数字从一个颜色分量(RR、GG、BB)溢出到另一个颜色分量(B->G、G->R)。但如果不是整数(R→G,G→B),它们也可以以其他方式携带。我假设 currentValue 是 0-100 之间的任意数字。

考虑一下,每个数字都有自己的颜色分量(以十进制表示):

5.0 * 111 = 555 = 5R, 5G, 5B
5.5 * 111 = 610.5 = 6R, 1G, 0.5B
percentLoaded.textColor = int((currentValue / 100) * 0xFF) * 0x010101;

The cast to an int before multiplying by 0x010101 is a must. 8 bit color values can only be integers between 0-255. If it's not within these bounds, the multiplication by 0x010101 could cause numbers to overflow from one color component(RR,GG,BB) to another (B->G, G->R). But they can also carry the other way if not an integer (R->G, G->B). I've assumed currentValue is any number between 0-100.

Consider this, with each digit it's own color component(in decimal):

5.0 * 111 = 555 = 5R, 5G, 5B
5.5 * 111 = 610.5 = 6R, 1G, 0.5B
计㈡愣 2024-08-25 18:39:12

如果这对你有用

percentLoaded.textColor=(currentValue/100)*0xFFFFFF;

那么你可以这样做

var percent = currentValue / 100;
percentLoaded.textColor = Math.floor(percent*0xFF) * 0x010101;

If this works for you

percentLoaded.textColor=(currentValue/100)*0xFFFFFF;

Then you could just do

var percent = currentValue / 100;
percentLoaded.textColor = Math.floor(percent*0xFF) * 0x010101;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文