Remapped number script

Hello friends
I needed to have the script of this algorithm in Python. I searched the forum but did not find anything. Is there anyone to write this simple script. I need to know how this algorithm works.
Thanks in advance

This here might help: mapValuesAsColors.py · GitHub

Found it here with google: remap numbers(Yes I know there is a GH component;)) – Grasshopper (grasshopper3d.com)

2 Likes

This is a ‘manual’ remap algorithm using basic GH components (add, subtract, multiply, divide). Included for comparison is the GH ‘remap’ component.

Manual remap.gh (17.8 KB)

2 Likes
import sys

def remap(value, source_min, source_max, target_min, target_max, clamp=False):
    """Remaps a numerical value from a source to a target domain, both being
        defined by minimum and maximum values. If the input value lies outside
        the target domain the output value will also depass it. This can be 
        prevented by clamping the output value, which ensures that it always
        stays within the target bounds.
    
    Args:
      value (int|float): A numerical value to remap
      source_min (int|float): A lower bound for the source domain 
      source_max (int|float): An upper bound for the source domain 
      target_min (int|float): A lower bound for the target domain 
      target_max (int|float): An upper bound for the target domain 
      clamp (bool): Optionally True, if the output value should be 
        clamped between [target_min, target_max), by default False
        
    Returns:
      The remapped value, or target_min if the absolute difference between
        source_min and source_max is less than sys.float_info.epsilon to
        prevent divide by zero.
    """
    if abs(source_min - source_max) < sys.float_info.epsilon:
        return target_min
    else:
        out_val = ((value - source_min) / (source_max - source_min)) * \
                  (target_max - target_min) + target_min

        if clamp:
            if target_max < target_min:
                if out_val < target_max:
                    out_val = target_max
                elif out_val > target_min:
                    out_val = target_min
            else:
                if out_val > target_max:
                    out_val = target_max
                elif out_val < target_min:
                    out_val = target_min
        
        return out_val

3 Likes