I was messing with the Color system in GASP and felt that there was room for improvement. I had used GASP's RGB colors from GASP Pygame and wanted to try integrating both RGB and HEX into the same system.
Here is the link to my improved version of GASP's color.py:
https://gitlab.com/RichardMartinez/csc200/-/blob/master/gasp/color_improved.py
And now to explain why it's better. To start, let's look at the current color system:
>>> from gasp import *
>>> color.BLACK
'#000000'
>>> color.black
AttributeError: module 'gasp.color' has no attribute 'black'
As shown here, the current color system is entirely case sensitive. This isn't too big of a problem if you know about it but I thought it would be more seamless if you could do this:
>>> from color_improved import *
>>> color.BLACK
'#000000'
>>> color.black
'#000000'
>>> color.bLaCk
'#000000'
This is a nice touch so I added it everywhere in my new version. Yes, every single method call in color_improved.py is NOT case-sensitive. This includes getting colors, setting the mode, and converting between systems.
In color_improved.py, to use a color object:
>>> from color_improved import *
>>> rgb_color = Color("RGB")
>>> hex_color = Color("HEX")
>>> rbg_color.black
(0, 0, 0)
>>> hex_color.black
'#000000'
To change between HEX and RGB, use set_mode():
>>> from color_improved import *
>>> color.black
'#000000'
>>> color.set_mode("RGB")
>>> color.black
(0, 0, 0)
>>> color.set_mode("HEX")
>>> color.black
'#000000'
This, of course, works with all 138 colors currently available in the color system and will still work should any new colors be added. Speaking of available, to see a full list of all available colors:
>>> from color_improved import *
>>> color.available()
[('ALICEBLUE', '#F0F8FF'), ('ANTIQUEWHITE', '#FAEBD7'), ('AQUA', '#00FFFF'), ...
>>> color.set_mode("RGB")
>>> color.available()
[('ALICEBLUE', (240, 248, 255)), ('ANTIQUEWHITE', (250, 235, 215)), ('AQUA', (0, 255, 255)), ...
Finally, there are static methods for converting RGB to HEX and vice versa. Since they are static methods, they don't require a color object to be instantiated, but will still work on an existing color object.
>>> from color_improved import *
>>> color.hex_to_rgb("#cd8234")
(205, 130, 52)
>>> color.hex_to_rgb("CD8234")
(205, 130, 52)
>>> color.rgb_to_hex((0, 0, 0))
'#000000'
>>> color.rgb_to_hex(0, 0, 0)
'#000000'
As shown, these methods were specifically designed to be error proof. In hex_to_rgb, the leading # is optional and letters are NOT case-sensitive (Nothing special about #CD8234 just an example). As for rgb_to_hex, it is designed to allow either a single passed three tuple or exactly three passed parameters. This is to avoid confusion among users who prefer one method over the other.
The last thing I was deciding on was whether to have two pre-defined color objects: "rgb_color" and "hex_color" or just the one "color". I commented out the first two but they are still there should you decide to use that idea. I ended up going with just the one "color" with it defaulting to HEX. The reason I chose this was to avoid breaking old GASP programs with a new name (hex_color) for essentially the same thing.
Overall, this version of color.py would be a significant improvement over what it currently is. Please feel free to plop it straight into the GASP codebase today. If something breaks or you have a question please let me know.
Looking forward to see how this helps!
I was messing with the Color system in GASP and felt that there was room for improvement. I had used GASP's RGB colors from GASP Pygame and wanted to try integrating both RGB and HEX into the same system.
Here is the link to my improved version of GASP's color.py:
https://gitlab.com/RichardMartinez/csc200/-/blob/master/gasp/color_improved.py
And now to explain why it's better. To start, let's look at the current color system:
```
>>> from gasp import *
>>> color.BLACK
'#000000'
>>> color.black
AttributeError: module 'gasp.color' has no attribute 'black'
```
As shown here, the current color system is entirely case sensitive. This isn't too big of a problem if you know about it but I thought it would be more seamless if you could do this:
```
>>> from color_improved import *
>>> color.BLACK
'#000000'
>>> color.black
'#000000'
>>> color.bLaCk
'#000000'
```
This is a nice touch so I added it everywhere in my new version. Yes, every single method call in color_improved.py is NOT case-sensitive. This includes getting colors, setting the mode, and converting between systems.
In color_improved.py, to use a color object:
```
>>> from color_improved import *
>>> rgb_color = Color("RGB")
>>> hex_color = Color("HEX")
>>> rbg_color.black
(0, 0, 0)
>>> hex_color.black
'#000000'
```
To change between HEX and RGB, use set_mode():
```
>>> from color_improved import *
>>> color.black
'#000000'
>>> color.set_mode("RGB")
>>> color.black
(0, 0, 0)
>>> color.set_mode("HEX")
>>> color.black
'#000000'
```
This, of course, works with all 138 colors currently available in the color system and will still work should any new colors be added. Speaking of available, to see a full list of all available colors:
```
>>> from color_improved import *
>>> color.available()
[('ALICEBLUE', '#F0F8FF'), ('ANTIQUEWHITE', '#FAEBD7'), ('AQUA', '#00FFFF'), ...
>>> color.set_mode("RGB")
>>> color.available()
[('ALICEBLUE', (240, 248, 255)), ('ANTIQUEWHITE', (250, 235, 215)), ('AQUA', (0, 255, 255)), ...
```
Finally, there are static methods for converting RGB to HEX and vice versa. Since they are static methods, they don't require a color object to be instantiated, but will still work on an existing color object.
```
>>> from color_improved import *
>>> color.hex_to_rgb("#cd8234")
(205, 130, 52)
>>> color.hex_to_rgb("CD8234")
(205, 130, 52)
>>> color.rgb_to_hex((0, 0, 0))
'#000000'
>>> color.rgb_to_hex(0, 0, 0)
'#000000'
```
As shown, these methods were specifically designed to be error proof. In hex_to_rgb, the leading # is optional and letters are NOT case-sensitive (Nothing special about #CD8234 just an example). As for rgb_to_hex, it is designed to allow either a single passed three tuple or exactly three passed parameters. This is to avoid confusion among users who prefer one method over the other.
The last thing I was deciding on was whether to have two pre-defined color objects: "rgb_color" and "hex_color" or just the one "color". I commented out the first two but they are still there should you decide to use that idea. I ended up going with just the one "color" with it defaulting to HEX. The reason I chose this was to avoid breaking old GASP programs with a new name (hex_color) for essentially the same thing.
Overall, this version of color.py would be a significant improvement over what it currently is. Please feel free to plop it straight into the GASP codebase today. If something breaks or you have a question please let me know.
Looking forward to see how this helps!