问题描述
我使用SFML,它有一个颜色函数,它获取RGB中的值。例如..(255,0,0)。
所以,如果我使用(76,204,63),那么我们可以通过循环循环这些数字,该功能将调整这3个数字。所以我需要的功能,以摄取rgb转换为HSV,然后返回rgb。
任何想法如何进行这个?
我希望使用的sfml代码是...
_sprite.setColor(76,204,63);这将把sprite设置为一种颜色...我想弄清楚这些数字是怎么做的,通过色调来循环颜色。
有了googling,我发现答案,并将代码转换为C ++与SFML心里。
我在相当不好地投射,所以随意让它更好。我想应该可以替换3x3数组。
sf :: Uint8 clampAndConvert(float v)
{
if(v< 0)
return 0;
if(v> 255)
return 255;
return static_cast< sf :: Uint8>(v);
}
sf :: Color RGBRotate(sf :: Color old,float degrees)
{
float cosA = cos(degrees * 3.14159265f / 180)
float sinA = sin(degrees * 3.14159265f / 180);
float rot = 1.f / 3.f *(1.0f-cosA)+ sqrt(1.f / 3.f)* sinA;
float rx = old.r *(cosA +(1.0f-cosA)/ 3.0f)+ old.g * rot + old.b * rot;
float gx = old.r * rot + old.g *(cosA + 1.f / 3.f *(1.0f-cosA))+ old.b * rot;
float bx = old.r * rot + old.g * rot + old.b * cosA + 1.f / 3.f *(1.0f-cosA);
return sf :: Color(clampAndConvert(rx),clampAndConvert(gx),clampAndConvert(bx),old.a);
}
编辑:删除了不必要的投射。 >
编辑:删除矩阵。
>因为我注意到代码并不是真正想要的工作,但这里是一个硬编码的解决方案,工作完美,只是不那么紧凑和漂亮。
#include< SFML / Graphics.hpp>
int main()
{
sf :: RenderWindow Screen(sf :: VideoMode(800,600,32),Game,sf :: Style :: Close );
Screen.setFramerateLimit(60);
sf :: RectangleShape rect(sf :: Vector2f(350.f,350.f));
rect.setPosition(150,150);
int dr = 0;
int dg = 0;
int db = 0;
sf :: Uint8 r = 255,g = 0,b = 0;
while(Screen.isOpen())
{
sf :: Event Event;
while(Screen.pollEvent(Event))
{
if(Event.type == sf :: Event :: Closed)
Screen.close();
}
r + = dr;
g + = dg;
b + = db;
if(r == 255& g == 0&& b == 0)
{
dr = 0; dg = 1; db = 0;
}
if(r == 255& g == 255&& b == 0)
{
dr = -1; dg = 0; db = 0;
}
if(r == 0& g == 255&& b == 0)
{
dr = 0; dg = 0; db = 1;
}
if(r == 0& g == 255&& b == 255)
{
dr = 0; dg = -1; db = 0;
}
if(r == 0& g == 0&& b == 255)
{
dr = 1; dg = 0; db = 0;
}
if(r == 255& g == 0&& b == 255)
{
dr = 0; dg = 0; db = -1;
}
rect.setFillColor(sf :: Color(r,g,b));
Screen.clear();
Screen.draw(rect);
Screen.display();
}
return 0;
}
I am using SFML and it has a color function that takes values in RGB. example.. (255,0,0). I would like to be able to cycle these numbers though a loop so that the displayed colour cycles though the hue...
So if I am using (76,204,63) the function will adjust those 3 numbers. So I need the function to intake rgb convert to HSV and then return rgb.
Any ideas how I would go about this?
the sfml code I wish to use is...
_sprite.setColor(76,204,63); This will set the sprite to a colour... I ma trying to work out how once that is done with those numbers to cycle the colour though the hue.
With a bit googling I've found this answer and converted the code to C++ with SFML in mind.I'm casting around pretty badly, so feel free to make it better. I guess it should even be possible to replace the 3x3 array.
sf::Uint8 clampAndConvert(float v)
{
if(v < 0)
return 0;
if( v > 255)
return 255;
return static_cast<sf::Uint8>(v);
}
sf::Color RGBRotate(sf::Color old, float degrees)
{
float cosA = cos(degrees*3.14159265f/180);
float sinA = sin(degrees*3.14159265f/180);
float rot = 1.f/3.f * (1.0f - cosA) + sqrt(1.f/3.f) * sinA;
float rx = old.r * (cosA + (1.0f - cosA) / 3.0f) + old.g * rot + old.b * rot;
float gx = old.r * rot + old.g * (cosA + 1.f/3.f*(1.0f - cosA)) + old.b * rot;
float bx = old.r * rot + old.g * rot + old.b * cosA + 1.f/3.f * (1.0f - cosA);
return sf::Color(clampAndConvert(rx), clampAndConvert(gx), clampAndConvert(bx), old.a);
}
Edit: Removed unnecessary casts.
Edit: Got rid of the matrix.
Edit: As I've noticed the code doesn't really work as wanted, but here's a hardcoded solution that works perfectly, just isn't that compact and nice.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow Screen (sf::VideoMode (800, 600, 32), "Game", sf::Style::Close);
Screen.setFramerateLimit(60);
sf::RectangleShape rect(sf::Vector2f(350.f, 350.f));
rect.setPosition(150, 150);
int dr = 0;
int dg = 0;
int db = 0;
sf::Uint8 r = 255, g = 0, b = 0;
while (Screen.isOpen())
{
sf::Event Event;
while (Screen.pollEvent (Event))
{
if (Event.type == sf::Event::Closed)
Screen.close();
}
r += dr;
g += dg;
b += db;
if(r == 255 && g == 0 && b == 0)
{
dr = 0; dg = 1; db = 0;
}
if(r == 255 && g == 255 && b == 0)
{
dr = -1; dg = 0; db = 0;
}
if(r == 0 && g == 255 && b == 0)
{
dr = 0; dg = 0; db = 1;
}
if(r == 0 && g == 255 && b == 255)
{
dr = 0; dg = -1; db = 0;
}
if(r == 0 && g == 0 && b == 255)
{
dr = 1; dg = 0; db = 0;
}
if(r == 255 && g == 0 && b == 255)
{
dr = 0; dg = 0; db = -1;
}
rect.setFillColor(sf::Color(r, g, b));
Screen.clear();
Screen.draw(rect);
Screen.display();
}
return 0;
}
这篇关于周期R,G,B值为HUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!