User Tools

Site Tools


en:tutorials:color:percentage2color

This is an old revision of the document!


Introduction

Ok this is not really a orx specific tutorial. It could easily used in any other environment. So what is it about, anyway? Ok so you have some object in your game, to which you want to assign a color according to some percentage. So let's say you have a health bar, that should be green, when it's full and red when it's almost empty. The following code will do this. It will scale the given percentage gradually to a color ranging from red (0 percent) over yellow(50 percent) to green(100 percent).

Implementation

In the following percentage is a floating point number ranging from 0.0 to 1.0 and col is the resulting color.

  orxDOUBLE percentage;
  orxCOLOR col;
  //do you want transparency?
  //col.fAlpha=.9f;
  col.vRGB.fX=(percentage<0.5 ) ? 1.0 : 1.0 - (percentage-0.5)*1.4;//R
  col.vRGB.fY=(percentage>=0.5 ) ? 1.0 : (percentage*1.4)+0.3;//G
  //lower the saturation:
  col.vRGB.fZ=0.25;//B
  //lower the brightness:
  col.vRGB.fX*=0.8;
  col.vRGB.fY*=0.8;

en/tutorials/color/percentage2color.1297264846.txt.gz · Last modified: 2017/05/30 00:50 (7 years ago) (external edit)