【问题标题】:How to create a function that returns color between 2 colors in 2d color picker?如何创建一个在 2d 颜色选择器中返回 2 种颜色之间颜色的函数?
【发布时间】:2015-07-08 07:02:26
【问题描述】:

我想创建一个 hp bar,当 hp 满时(scale=1),rgb 为 100,200,255,(如亮绿色),当 hp 为 0(scale=0)时,rgb 为 100,50,0 (深红色):

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) 将返回类似黄色的东西,黄色是颜色选择器中起始颜色和结束颜色之间的颜色。

【问题讨论】:

  • 你可能想编辑你的帖子并添加一个编程语言标签,否则没有人会找到或阅读这个问题。
  • 我认为您正在尝试以奇怪的方式执行此操作。我建议你另外两个解决方案: 1. 用渐变填充 hp bar(如何创建,你可以用谷歌搜索)并只显示这个矩形的一部分。 2.只需选择三、四种颜色并设置矩形的颜色。 (例如,大于 50% 为绿色,小于 50% 为黄色,小于 15% 为红色,小于 5% 为深红色)。渐变例如:stackoverflow.com/questions/521493/…
  • Paolo M,你为什么要问框架?他询问有关颜色算法的数学问题,这与框架无关。他问如何得到答案:Color A and Color B to get (Color A + Color B)/2 but in RGB space.
  • 如果您正在查看颜色选择器,那么在 RGB 中执行颜色“平均”可能不正确,而是使用 HSV 变体。 en.wikipedia.org/wiki/HSL_and_HSV

标签: c++ math colors


【解决方案1】:

这取决于您的 Color 课程。我假设你有一个简单的Color 类,它需要 3 个整数(分别是红绿蓝)。这也假设您可以将您的 HP 作为百分比传递(这是有道理的,因为此函数不一定知道 50hp 是多少,因为最大 HP 可能取决于怪物等)

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 at 0% hp
    cout << GetHPColor(0.5); // 127, 127, 0 at 50% hp
    cout << GetHPColor(1.0); // 0, 255, 0 at 100% hp
    return 0;
}

您选择的颜色可以不同。这是为了向您提供如何做到这一点的一般要点。

【讨论】:

  • 正是我的想法,唯一缺少的是,纯红色不应该是可见的(没有生命,没有生命值条)所以你可能需要计算一个偏移量来获得一个整洁的渐变跨度>
猜你喜欢
  • 2017-05-04
  • 2022-10-16
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
相关资源
最近更新 更多