ReMap

From Multi Theft Auto: Wiki
Revision as of 20:46, 2 August 2017 by Jayceon (talk | contribs) (Created page with "Re-maps a number from one range to another. ==Syntax== <syntaxhighlight lang="lua">int/float reMap( float value, float low1, float high1, float low2, float high2)</syntaxhigh...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Re-maps a number from one range to another.

Syntax

int/float reMap( float value, float low1, float high1, float low2, float high2)

Required Arguments

  • value: The incoming value to be converted
  • low1: Lower bound of the value's current range
  • high1: Upper bound of the value's current range
  • low2: Lower bound of the value's target range
  • high2: Upper bound of the value's target range

Returns

Returns the re-mapped number.

Code

function reMap(value, low1, high1, low2, high2)
    return low2 + (value - low1) * (high2 - low2) / (high1 - low1)
end

Example

Click to collapse [-]
Clientside Example
local realAlpha = 127.5
local alpha = reMap(realAlpha, 0, 255, 0, 1)

-- And the "alpha" return 0.5 because 255/2 = 127.5 and 0 to 1 range is 0.5

-- Reverse:
local alpha = 0.5
local realAlpha = reMap(realAlpha, 0, 1, 0, 255)

-- And the "realAlpha " return 127.5 because 1/2 = 0.5 and 0 to 255 range is 127.5