Jump to content

Color change depending on health


Poltergeist Azarov
 Share

You are about to reply to a thread that has been inactive for 3407 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hello, I am making a combat meter and wanted its color of floating text to be changed depending on health ratio. There gonna be three main colors as green, yellow and red. As much as agent's energy gets low floating text color needs to be changed. I tried some ways to do and failed to code something compact for this operation. Any ideas?

 

Edit: since colors are gonna be used depending on health ratio, color might seen as orange sometimes when agent's health is about %30. I failed coding it this perfect way.

Link to comment
Share on other sites

In Psuedocode:-

floating_text = "Floating text"health = HealthyIf health = Healthy then     SetText(floating_text,<0,0,1>) //Green textElse If health = Sickly then     SetText(floating_text,<1,1,0>) //Yellow textElse     SetText(floating_text,<1,0,0>) //Red text
End If

 Remember that in SL, colours are set as <Red, Green, Blue> in vector form. Therefore:-

Green = <0,1,0>Yellow = <1,1,0>Orange = <1,0.5,0>Red = <1,0,0>

You don't need to use the 0-1 range for colours, can also use 0-255.

For writing in LSL, see also: llSetText

Link to comment
Share on other sites

You can try this formula:

vector color_dead = <1,0,0>;    // color for nearly deadvector color_healthy = <0,1,0>;	// color for 100% healthfloat  health_level;            // 0-100......	vector color_level = (color_healthy - color_dead) * (health_level/100.0) + color_dead;......

 The color will float between color_dead (0%) and color_healthy (100%)

It's a linear interpolation. If you dont like the colors, nothing stops you from enhancing it and set a 50% color for example.

Link to comment
Share on other sites

I was trying to code something that can slightly change the color instead of changing at each %30 decrease of the health. For example this code below changes given color to black slighty:

integer FTC=0;Uncolorize(vector color) {    integer x;    float CX=color.x; float CY=color.y; float CZ=color.z;    for (x=1; x<=10; ++x) {        if (CX>0.0) { CX-=0.1; }        if (CY>0.0) { CY-=0.1; }        if (CZ>0.0) { CZ-=0.1; }        llSetLinkPrimitiveParamsFast(3,[PRIM_COLOR, FTC, <CX,CY,CZ>, 0.99]);        llSleep(0.022);    }}

 Another example of a slight color change to given color:

Colorize(vector color) {    vector NewColorVar=<1.0, 1.0, 0.0>;    vector red=    <1.0, 0.0, 0.0>;    vector green=  <0.0, 1.0, 0.0>;    vector cyan=   <0.0, 1.0, 1.0>;    vector yellow= <1.0, 1.0, 0.0>;        integer x;     for (x=1; x<=10; ++x) {        if (color==red)   { NewColorVar+=<0.0,-0.1,0.0>; }        if (color==green) { NewColorVar+=<-0.1,0.0,0.0>; }        if (color==cyan)  { NewColorVar+=<-0.1,0.0,0.1>; }        llSetLinkPrimitiveParamsFast(3,[PRIM_COLOR, FTC, NewColorVar, 0.99]);        llSleep(0.022);    }}

I've coded these both but now I am really stuck at changing between three colors slightly. 

Link to comment
Share on other sites

I'm sure there are plenty of open source scripts that do this, but I'm too lazy to look for one now.

For values of health scaled to the range 0.0 to 1.0, you can probably get by using a color vector of simply

<1.0-health, health, 0.0> * 2.0

because a color is automatically range-constrained to no more than 1.0 for R, G, and B. That will however stick more in the mid-range of yellow than a more "accurate"

<1.0-health, health, 0.0> * (2.0 - 2.0*llFabs(0.5-health))

which scales the RGB dimensions into 1.0 ranges.

I have no idea what other scripts do. Maybe they're more clever.

Link to comment
Share on other sites


Poltergeist Azarov wrote:

Played alot with it and sadly failed to change between three colors :\ do you have a suggestion for that

For 3 colors it's the same, you just split it into a 0-50 and a 50-100 range

vector color_dead = <1,0,0>;    // color for nearly deadvector color_50 = <1,1,0>;vector color_healthy = <0,1,0>;	// color for 100% healthfloat  health_level;            // 0-100......	vector color_level;	if (health_level<50.0) color_level = (color_50 - color_dead) * (health_level/100.0) + color_dead;	                  else color_level = (color_healthy - color_50) * (health_level/100.0) + color_50;......

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3407 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...