Jump to content

[help]calculation with colors[solved]


#RooTs

Recommended Posts

hello friends, I do not know how to do this calculation, I have my script below, that. the color white is going to the red.

I want to color 01 going, to color, 02

01.png

01 - color(103, 188, 107)

02.png

02 - color(255, 0, 0)

function getHealthColor() 
    local health = getElementHealth(localPlayer) 
        
    if (health) then 
        local r = 255 
        local g = (255/100) * health 
        local b = (255/100) * health 
      
        return r, g, b 
    else 
        return 0, 255, 0 
    end 
end 

Edited by Guest
Link to comment
function getHealthColor() 
    local health = getElementHealth(localPlayer) 
  
    if (health) then 
        local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 100, "Linear" ) 
  
        return r, g, b 
    else 
        return 0, 255, 0 
    end 
end 

Enjoy :)

Link to comment
function getHealthColor() 
    local health = getElementHealth(localPlayer) 
  
    if (health) then 
        local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 100, "Linear" ) 
  
        return r, g, b 
    else 
        return 0, 255, 0 
    end 
end 

Enjoy

@Tete omar, Thanks man, and Health in 200% ??

local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 200, "Linear" ) 

I tried the following

function getHealthColor() 
    local health = getElementHealth(localPlayer) 
  
    if (health) then 
        local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 100, "Linear" ) 

and

function getHealthColor2() 
    local health = getElementHealth(localPlayer) 
  
    if (health) then 
        local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 200, "Linear" ) 

more when the getHealthColor2 is at 100% it turns orange

Link to comment
  • MTA Team
  
function getVehicleHealthColor(vehicle) 
    if (not isElement(vehicle)) then return end 
    if (getElementType(vehicle) ~= "vehicle") then return end 
    local health = getElementHealth(vehicle) 
    local f = math.max(0, math.min(1, health / 1000)) 
    return math.min(255, (510 * (1-f))), math.min(255, 510 * f), 0 
end 
  

Link to comment

an example of how you can do this:

function getHealthColor() 
    local healthRelative = health*rate/100 
    local health = getElementHealth(localPlayer) 
    local rate =  500/getPedStat(localPlayer,24) 
    local v = health*rate 
    return (100-v)*2.55, (v*2.55), 0 
end 

Link to comment
@Mizudori, the calculation of @Tete omar worked perfectly in 100% Health

now at 200% non

look at my post

function getHealthColor() 
    local health = getElementHealth(localPlayer) 
  
    if (health) then 
        local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 100, "Linear" ) 
  
        return r, g, b 
    else 
        return 0, 255, 0 
    end 
end 

Enjoy :)

you forget use getPedMaxHealth to get max health

Link to comment
more when the getHealthColor2 is at 100% it turns orange

If you tried to understand the examples given to you, and read about the functions they used - you would know why.

interpolateBetween has a progress argument, where you need to provide a float between 0 and 1. In this case a progress of 1 would mean full health, green.

100/100 = 1.0 (100%)

100/200 = 0.5 (50%)

That's why it showed as orange when at 100%.

Link to comment
  • MTA Team
  
function getPlayerHealthColor(player) 
    if not isElement(player) or getElementType(player) ~= "player" then 
        return 255, 0, 0 
    end 
  
    local health = math.max(0, getElementHealth(player)) 
    local stat = getPedStat(player, 24) 
    local maxhealth = math.max(1, 100 + (stat - 569) / 4.31) 
  
    local f = math.max(0, math.min(1, health / maxhealth)) 
    return math.min(255, (510 * (1-f))), math.min(255, 510 * f), 0 
end 
  

Link to comment

Try this one, i'm not sure:

function getHealthColor ( ) 
  
    local progress = getElementHealth ( localPlayer ) / -- getPedMaxHealth ( localPlayer ) 
    -- getPedMaxHealth server-sided, and triggered. 
  
    local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, progress, "Linear" ) 
    return r, g, b 
end 
  
  

Link to comment
getPedMaxHealth should work clientside too, because getPedStat is a client- and serverside function.

Probably works(i've never tried before), try it LekRoots.

  
function getHealthColor ( ) 
  
    local progress = getElementHealth ( localPlayer ) / getPedMaxHealth ( localPlayer ) 
  
    local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, progress, "Linear" ) 
    return r, g, b 
end 
  
function getPedMaxHealth(ped) 
    local stat = getPedStat(ped, 24) 
    local maxhealth = 100 + (stat - 569) / 4.31 
    return math.max(1, maxhealth) 
end 

Link to comment

managed to solve with the function of ( RageScripting/FuriouZ and Tete Omar)

@FuriouZ, without want, you helped me :lol: Thanks man

if ( playerHealth  <= 50) then 
                HP_Colour = tocolor(200, 0, 0, 190) 
                HP_Alpha = tocolor(200, 0, 0, 100) 
            else 
                HP_Colour = tocolor(102, 204, 102, 190) 
                HP_Alpha = tocolor(102, 204, 102, 100)               
            end 

My function

  
function getHealthColor() 
    local health = getElementHealth(getLocalPlayer()) 
  
    if (health) then 
        local r, g, b = interpolateBetween ( 255, 0, 0, 103, 188, 107, health / 50, "Linear" ) 
  
        return r, g, b 
    else 
        return 0, 255, 0 
    end 
end 
  
--within the render 
local sz1, sz2, sz3 = getHealthColor() 
local health = math.floor( getElementHealth( getLocalPlayer() )) 
if ( health  <= 50) then 
                HP_Colour = tocolor(sz1, sz2, sz3, 255) 
                HP_Alpha = tocolor(sz1, sz2, sz3, 100) 
            else 
                HP_Colour = tocolor(103, 188, 107, 255) 
                HP_Alpha = tocolor(103, 188, 107, 100)               
            end 
  
local stat = getPedStat ( getLocalPlayer(), 24 ) 
if stat < 1000 then 
dxDrawRectangle(sW*72, sH*698, sW*115, sH*8, HP_Alpha, false) --fundo 
dxDrawRectangle(sW*72, sH*698, sW*115/100*health, sH*8, HP_Colour, false) 
else 
dxDrawRectangle(sW*72, sH*698, sW*115, sH*8, HP_Alpha, false) --fundo 
dxDrawRectangle(sW*72, sH*698, sW*115/200*health, sH*8, HP_Colour, false) 
end 
  

@FuriouZ, if you want to use my calculation, stay the will :lol::lol:

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...