I am generating a derived class, and would like this to treat one parameter of the base class differently from how the base class treats this. In particular, I want color RGB values, which in the base class are expected to be from 0-1, to be given on the conventional 0-255 scale.
I have tried this, not really expecting it to work, and it did not. Is there any way to modify how a class interprets the base class’s own initializing variables?
from vpython import sphere, vector
def __init__(self, color):
self.color = color/255
P.S. I realize it would be easier to create a wrapper for the color value, as below, but am still curious if the above is possible.
That looks good! Yeah, I wasn’t sure how it would be formatted, but that’s decent. BTW, you can simplify that condition slightly - instead of asking if “color” is among the dictionary’s keys, you can just ask if “color” is in the dictionary itself
if "color" in kwargs:
kwargs["color"] /= 255
I would still recommend checking whether you want to divide by 255 or by 256, and whether you want to use /= or //=, but that looks like a pretty decent way to do it.
Exactly. Color computations are a lot easier with a normalized float in the range 0.0…1.0. And they are more precise as well, because the float offers a (practically) continuous range of values, while e.g. uint8 has only 256 discrete values. And thus 100% intensity of some component is 255, and 50% is… 128: not very accurate.