c++ - How to create a function that returns color between 2 colors in 2d color picker? -
i want create hp bar,when hp full (scale=1), rgb 100,200,255, (like bright green) , when hp 0 (scale=0), rgb 100,50,0 (dark red):
void gethpbarcolor(int startr,int startg,int startb,int endr,int endg,int endb,float scale);
which gethpbarcolor(100,200,255,100,50,0,0.5) (half hp) return yellow, yellow color between start color , end color in color picker.
it depends on color
class. have assumed have simple color
class takes 3 ints (red green blue respectively). assumes can pass hp percent (which makes sense, since function won't know how 50hp since max hp may dependent on monster etc.)
struct color { int red; int green; int blue; color(int r, int g, int b) : red(r), green(g), blue(b) {} friend std::ostream& operator << (std::ostream& oss, const color& clr) { oss << clr.red << ", " << clr.green << ", " << clr.blue << endl; return oss; } }; color gethpcolor(double dpercent) { return color(255-(255*dpercent), 0 + 255*dpercent, 0); } int main() { cout << gethpcolor(0.0); // 255, 0, 0 @ 0% hp cout << gethpcolor(0.5); // 127, 127, 0 @ 50% hp cout << gethpcolor(1.0); // 0, 255, 0 @ 100% hp return 0; }
the colors choose can different. give general gist of how it.
Comments
Post a Comment