英文:
how can I change RGB values according to percentage
问题
我想根据百分比更改RGB值。意味着0%应该显示红色,50%应该是绿色,100%将是蓝色。我正在使用FastLED。我尝试了这样的代码,但没有得到最佳结果。有人能建议一些更好的东西吗?
int R, G, B;
int p = percentage;
if (p >= 0 and p <= 50) {
R = abs(p - 100);
G = p * 2;
}
if (p > 50 and p <= 100) {
G = abs(p - 100);
B = p * 2;
}
// 也尝试过下面的代码
R = abs(p - 100);
G = p / 2;
B = p;
leds[0] = CRGB(R, G, B);
FastLED.show();
英文:
I want to change RGB values according to the percentage. means 0% should show Red , 50% should green and 100% will Blue color.I am working on fastled.I tried like this but didnt get the best result.can anyone suggest some good stuf??
int R,G,B;
int p = percentage;
if(p >= 0 and p <= 50){
R = abs(p - 100);
G = p*2;
}
if(p > 50 and p <= 100){
G = abs(p - 100);
B = p*2;
}
>! also tried
R = abs(p-100);
G = p/2;
B = p;
leds[0] = CRGB(R,G,B);
FastLED.show();
答案1
得分: 1
需要将百分比值转换为8位二进制值,即将范围在[0,100]内的值转换为范围在[0,255]内的值(在十六进制中是[0x00,0xFF])。
简单的缩放操作如下:
int r = pR * 255 / 100; // 百分比红色转换为十六进制
或者等效地:
int r = pR * 0xFF / 100; // 百分比红色转换为十六进制
从十六进制值到百分比的相反转换只是相反的操作。
请注意,由于只有101个百分比值,当进行此转换时,您不会获得256个可能的8位十六进制值中的所有值,但这应该足够接近。
英文:
You need to convert percentage values to 8-bit binary values, i.e., convert values in the range [0,100] into values in the range [0,255] (which is [0x00,0xFF] in hex).
A simple scaling operation does this:
int r = pR * 255 / 100; // percentage red to hex
or equivalently:
int r = pR * 0xFF / 100; // percentage red to hex
The opposite conversion, from hex value to percentage, is just the reverse operation.
Note that since there are only 101 percentage values, you won't get all of the 256 possible 8-bit hex values when you do this conversion, but it should be close enough.
答案2
得分: 0
从你的问题描述中,你可能想要像这样生成sRGB颜色范围内的RGB颜色,从红色到蓝色逆时针旋转。
#include <array>
#include <string>
#include <cmath>
#include <iostream>
std::array<uint8_t, 3> getcolorpercent(double percent)
{
std::array<uint8_t, 3> rgb{};
int segment{static_cast<int>(percent/25)};
double percent_f = .01 * (percent - 25 * segment);
double col0{ 1 }, col1{ 1 };
if (segment % 2 == 0)
col1 = sqrt(4 * percent_f);
else
col0 = sqrt(1 - 4 * percent_f);
rgb[(segment / 2) % 3] = static_cast<uint8_t>(std::round(255*col0));
rgb[(1 + segment / 2) % 3] = static_cast<uint8_t>(std::round(255 * col1));
return rgb;
}
int main()
{
auto print = [](const std::array<uint8_t, 3> rgb, std::string descr) {
// 注意:0+... 用于将uint8_t转换为int,以避免将其解释为char
std::cout << descr << " red:" << 0+rgb[0] << " green:" << 0+rgb[1] << " blue:" << 0+rgb[2] << '\n';
};
std::array<uint8_t, 3> rgb_red = getcolorpercent(0);
std::array<uint8_t, 3> rgb_orange = getcolorpercent(15);
std.array<uint8_t, 3> rgb_yellow = getcolorpercent(25);
std::array<uint8_t, 3> rgb_cyan = getcolorpercent(75);
std::array<uint8_t, 3> rgb_violet = getcolorpercent(130);
print(rgb_red, "red=");
print(rgb_orange, "orange=");
print(rgb_yellow, "yellow=");
print(rgb_cyan, "cyan=");
print(rgb_violet, "violet=");
}
输出:
red= red:255 green:0 blue:0
orange= red:255 green:198 blue:0
yellow= red:255 green:255 blue:0
cyan= red:0 green:255 blue:255
violet= red:255 green:0 blue:228
这会创建一个(反转的)从红色到蓝色的彩虹类型,范围从0%到100%。此外,它已扩展以允许百分比超过100,从蓝色->紫罗兰->紫色再回到红色。以下是从0到100百分比创建的图像:
英文:
From your problem statement, you probably want something like this which generates RGB colors counter-clockwise around the sRGB color gamut from red to blue.
#include <array>
#include <string>
#include <cmath>
#include <iostream>
std::array<uint8_t, 3> getcolorpercent(double percent)
{
std::array<uint8_t, 3> rgb{};
int segment{static_cast<int>(percent/25)};
double percent_f = .01 * (percent - 25 * segment);
double col0{ 1 }, col1{ 1 };
if (segment % 2 == 0)
col1 = sqrt(4 * percent_f);
else
col0 = sqrt(1 - 4 * percent_f);
rgb[(segment / 2) % 3] = static_cast<uint8_t>(std::round(255*col0));
rgb[(1 + segment / 2) % 3] = static_cast<uint8_t>(std::round(255 * col1));
return rgb;
}
int main()
{
auto print = [](const std::array<uint8_t, 3> rgb, std::string descr) {
// note: 0+... is to convert uint8_t to int to precent interpreting as char
std::cout << descr << " red:" << 0+rgb[0] << " green:" << 0+rgb[1] << " blue:" << 0+rgb[2] << '\n';
};
std::array<uint8_t, 3> rgb_red = getcolorpercent(0);
std::array<uint8_t, 3> rgb_orange = getcolorpercent(15);
std::array<uint8_t, 3> rgb_yellow = getcolorpercent(25);
std::array<uint8_t, 3> rgb_cyan = getcolorpercent(75);
std::array<uint8_t, 3> rgb_violet = getcolorpercent(130);
print(rgb_red, "red=");
print(rgb_orange, "orange=");
print(rgb_yellow, "yellow=");
print(rgb_cyan, "cyan=");
print(rgb_violet, "violet=");
}
Output:
red= red:255 green:0 blue:0
orange= red:255 green:198 blue:0
yellow= red:255 green:255 blue:0
cyan= red:0 green:255 blue:255
violet= red:255 green:0 blue:228
This creates a (reversed) rainbow type from red to blue for 0% to 100%. Additionally, this has been expanded to allow percentages to exceed 100 which can be used to produces colors going from blue->violet->purple and back to red. Here's an image created from this going from percent 0 to 100:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论