remap numbers(Yes I know there is a GH component;))

Hello,

i want to learn python so i thought begin with remaoing numbers cause when iam using it in gh i need mostly the remap,bounds and create domain so i tought it would be possible doing it in one python component. i found some code and it gets me no error message but neither an solution.

Maybe someone is willed to help me?

the code i put is:

def remap( x, oMin, oMax, nMin, nMax ):

    #range check
    if oMin == oMax:
        print "Warning: Zero input range"
        return None

    if nMin == nMax:
        print "Warning: Zero output range"
        return None

    #check reversed input range
    reverseInput = False
    oldMin = min( oMin, oMax )
    oldMax = max( oMin, oMax )
    if not oldMin == oMin:
        reverseInput = True

    #check reversed output range
    reverseOutput = False   
    newMin = min( nMin, nMax )
    newMax = max( nMin, nMax )
    if not newMin == nMin :
        reverseOutput = True

    portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
    if reverseInput:
        portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)

    result = portion + newMin
    if reverseOutput:
        result = newMax - portion

    
    
    return result
    print result
    
    a = result

thanks in advance!

Load Previous Replies
  • up

    Chapulin Colorado

    i have another question,

    if i want to do it like the gh component, to access the min and max values, i input the domain, how can i acces these values? is it like a list?would it be the first and second item? how would be the procedure?

    thanks in advance!

    4
  • up

    Daniel González Abalde

    You have to set the type of object that collects all parameter.

    Set Type Hint to Interval. 

    1
    • up

      Anders Holden Deleuran

      Here's the function I usually use to generate color gradients, it demonstrates a similar method for remapping values. Might help as well :)

      1