pythonarcade / arcade

Easy to use Python library for creating 2D arcade games.
http://arcade.academy
Other
1.7k stars 320 forks source link

Store Sprite Colors, and all others, as 4x floats, not 0-255 ints #1794

Open cspotcode opened 1 year ago

cspotcode commented 1 year ago

Enhancement request:

What should be added/changed?

BasicSprite._color should be stored as floats internally, not as ints from 0 to 255.

What would it help with?

A list of reasons discussed here: https://discord.com/channels/458662222697070613/458662458198982676/1110348630956720158

Color manipulation within python is easier and has greater precision w/floats. Useful for smoothly lerping between colors using float math instead of imprecise int math.

Easier for consumers to think about. More performant(?) since we never need to convert back and forth between int and float, only copy the float values to the GPU. A bit more data transfer.

Shaders already convert the ints to floats anyway.

cspotcode commented 1 year ago

Related to #1772

einarf commented 1 year ago

I really want these to be floats because of the flexibility this adds, but I'm not entirely sure yet if this is worth it. At least I don't think there's a fast way to have backwards compatible support for both byte and float colors. Worst case it could be something we consider futher down the line. Some more thinking is needed here.

We do have some places were float colors are used, so having the type certainly doesn't hurt.

pushfoo commented 1 year ago

I see three orthogonal concerns:

  1. lerping and intermediate generation, also solvable by generators or other internal error-tracking approaches
  2. Speed comparisons of color types & annotations (NamedTuple vs Tuple, protocol vs tuple subclass, etc), answerable with benchmarks
  3. Internal storage / object.color_float property interaction with object.color

The most urgent issue for me is the last one since it determines whether I should continue work on #1772.

  1. Do we have an efficient & clean way to handle lazily calculating the RGBA255 object.color property?
  2. In the absence of anything better, is object._color_dirty + properties acceptable?