Milo Gertjejansen
Milo Gertjejansen

Reputation: 511

TypeError for >> on float and int

I have some code below:

color = complexity * (255 / iterationCap)
r = (color >> 16) & 255
g = (color >> 8) & 255
b = (color >> 0) & 255

I am trying to calculate the color from a floating point number I get from the color variable.

Currently, I am using python 3.3 to try to shift the bits and and them with 255 to get the correct r, g, and b values.

The error I am getting is:

TypeError: unsupported operand type(s) for >>: 'float' and 'int'
  1. Is there an easy way to fix this? Like, can I shift floats with an int? And,
  2. If not, is there an easy way to calculate color from a number?

Currently I am using the image library to draw pixels to a file and I just tack my color tuple on to an array, which I then feed into Image.putdata(..).

Upvotes: 3

Views: 9582

Answers (3)

Tadeusz A. Kadłubowski
Tadeusz A. Kadłubowski

Reputation: 8335

In Python 3 the / operator is a floating point division. You want to use // for integer division.

Given your comments about what the code is supposed to do we can write something like:

color = int(complexity * 255 / iterationCap) # gives an integer number from 0 to 255
r = color >> 16
g = color >> 8
b = color

That creates a gray gradient as complexity changes.

Upvotes: 7

UltraInstinct
UltraInstinct

Reputation: 44434

Convert into an int before trying to attempt bitwise operations.

color = int(complexity * (255 / iterationCap))

Upvotes: 0

Steve Barnes
Steve Barnes

Reputation: 28370

color = long(complexity * (255 / iterationCap))

Since bit shifting floats is an undefined operation.

Upvotes: 0

Related Questions