在两种颜色之间进行插值的最有效方法是什么? (预计有伪代码和按位运算)
制作一个黑莓应用程序,想要一个渐变类。插入两种颜色的最有效方法(例如速度和电池寿命)是什么?请具体说明。
// Java, of course
int c1 = 0xFFAA0055 // color 1, ARGB
int c2 = 0xFF00CCFF // color 2, ARGB
float st = 0 // the current step in the interpolation, between 0 and 1
帮助从这里开始。 我应该分离每种颜色的每个通道,将它们转换为十进制并进行插值吗?有没有更简单的方法?
interpolatedChannel = red1+((red2-red1)*st)
interpolatedChannel = interpolatedChannel.toString(16)
^ 这是正确的做法吗?如果速度和效率 在移动应用程序中很重要,我应该使用按位运算吗?
帮我!
Making a Blackberry app, want a Gradient class. What's the most effective way (as in, speed and battery life) to interpolate two colors? Please be specific.
// Java, of course
int c1 = 0xFFAA0055 // color 1, ARGB
int c2 = 0xFF00CCFF // color 2, ARGB
float st = 0 // the current step in the interpolation, between 0 and 1
Help from here on.
Should I separate each channel of each color, convert them to decimal and interpolate? Is there a simpler way?
interpolatedChannel = red1+((red2-red1)*st)
interpolatedChannel = interpolatedChannel.toString(16)
^ Is this the right thing to do? If speed and effectiveness
is important in a mobile app, should I use bitwise operations?
Help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新了我的答案(找到了更好的方法):
以下技术将每个通道损失 1 位精度,但速度非常快,因为您不必分割颜色进入通道:
因此,首先您将两种颜色
0xFEFEFEFE
AND。这会删除每个通道的最后一位(降低精度,正如我所说)。之后,您可以安全地将整个值除以 2(实现为右移除 1)。最后,您只需将两个值相加即可。Updated my answer (found a better way):
The following technique will lose 1 bit precision per channel, but it's extremely fast, since you won't have to split the colors into channels:
So, first you AND both colors by
0xFEFEFEFE
. This removes the last bit per channel (reduces precision, as I said). After that, you can safely divide the entire value by 2 (implemented as a right-shift by 1). Finally, you just add up the two values.只是 /u/Quasimondo 答案的 java 版本:
如果您只需要精确的 50/50 比率,则可以删除位移:
Just the java version of /u/Quasimondo's answer:
If you only need exact 50/50 ratios you can cut out the bitshifting:
您必须分离通道,但无需将它们转换为十进制。
例如,如果您允许 256 个可能的梯度:
编辑:既然您说您对位管理不太了解,那么这里有一个分割通道的快速方法:
并将它们组合回来:
从这里开始,细节通常应该由编译器处理优化。如果有的话,您正在寻找最好的算法。
You'll have to separate channels, but there's no need to convert them to decimal.
For example, if you allow 256 possible gradients:
EDIT: Since you said you don't know much about bit management, here's a quick way to split channels:
And combining them back:
From here, the details should normally be handled by the compiler optimizations. If anything, you're looking for the best algorithm.
不确定这是否是最紧凑的方法,但与首先将它们分割成 3 个通道的经典方法相比,它使用的局部变量和运算符更少。
哦 - 抱歉,这是 Actionscript,但应该清楚如何将其转换为 Java。
Not sure if this is the most compact way of doing it, but it uses less local variables and less operators than the classic method that splis them into 3 channels first.
Oh - and sorry that this is Actionscript, but it should be clear how to convert this to Java.