1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

normalizing a number

Posted by _brentonW on Jul 23, 2009

I'm posting this here because I have been searching nodebox for information on generating floating point and integer numbers and have been unsuccessful. I have solved my problem. Hope it helps others. If there are other approaches to this problem please add your info.

Problem:
Want to convert a photoshop hue (degrees) into a usable nodebox normalized 0.0 to 1.0 system for colormode(HSB).

Solution:
Create a function that takes your number to be converted, multiplies it by 1 important, and divides by Photoshop's number system (0 to 359 degrees).

Example:
hue*1.0/359

size(200, 200) 
colormode(HSB)
 
#function converts photoshop degree system (0 to 359 degrees) to normalized range of 0 to 1
def convertHue(hue):
	nHue = hue*1.0/359
	return nHue
 
#hue degree to be converted
hueDegree = convertHue(43)
orange = color(hueDegree, .80, 0.95)
 
background(orange)
Note: You must multiply the number to be converted by 1 in order for a floating point number to be returned.

I would love for someone to explain why. This is different from Processing which requires a data type identifier.
Hope this is helpful to others.


 
Posted by Tom De Smedt on Jul 23, 2009

To elaborate on this, you need to multiply by 1.0, not just 1.

print 1 * 43 / 359 # yields 0
print 1.0 * 43 / 359 # yields 0.119777158774
Python has no explicit data types, it will just use whatever is appropriate. So if you start with a float (1.0) and multiply it with 43, you still have a float.

You could also divide 43 by 359.0 - then 43 will be cast to a float because you can't divide an integer by a float.

You can also do this by using the float() function in Python:
print float(43) / 359 # yields 0.119777158774
There's also an int() function:
print int(1.0) # yields 1
print int("1") # yields 1
To check what kind of data is stored in a variable, you can use Python's isinstance() function:
print isinstance(43, int)   # yields True
print isinstance(43, float) # yields False
print isinstance("a", str)  # yields True



Posted by _brentonW on Jul 23, 2009

Tom - thanks for the clarification and additional information. Very helpful.