-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get color based on name using Enum #52
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good except I don't think we should update the cycler which needs to be pretty immutable or users may get unwanted behavior.
@bobleesj please can you add a news item and update docs with this? It is really great. Ideally we would need a plot in the docs that shows the colors with the names. This is also needed to fine-tune the names. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please see inline.
news/colorupdate.rst
Outdated
|
||
**Changed:** | ||
|
||
* change of two colors for higher contrast: ``#AB987A`` to ``#918770`` and ``#984B43`` to ``#6C5050`` | ||
* Change of two colors for higher contrast: ``#AB987A`` to ``#918770`` and ``#984B43`` to ``#6C5050`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it appear that colors have changed, but no colors should have changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No colors have changed. Just captialize the first alphabet from "c" to "C"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no colors have changed, please delete these change from the news.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please review my PR.
- Updated doc with instruction
- Updated the example image legends from hex code to color name
It appears that the CI is failing perhaps due to cache.
I checked from my own repo CI that it runs.
https://github.com/bobleesj/bg-mpl-stylesheets/actions/runs/10475957028
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bobleesj this is really great. I love what you did here. Please see my inline comments and we can move to merge this. I really like the new plot of the colors.
Thanks for uppercasing all the hex's too....
news/colorupdate.rst
Outdated
|
||
**Changed:** | ||
|
||
* change of two colors for higher contrast: ``#AB987A`` to ``#918770`` and ``#984B43`` to ``#6C5050`` | ||
* Change of two colors for higher contrast: ``#AB987A`` to ``#918770`` and ``#984B43`` to ``#6C5050`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no colors have changed, please delete these change from the news.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please review @sbillinge
Again, CI passes from my forked repo:
https://github.com/bobleesj/bg-mpl-stylesheets/actions/runs/10479038124
- properties
lower_cased
instead oflower-cased
since it's a property. - news on color change removed.
assert hex == expected_hex | ||
|
||
|
||
@pytest.mark.parametrize( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, get_color_name(hex)
could instead return a string of bg-color
instead of bg_color
but the user probably would be storing the color name string and use that to get the hex code (maybe)
Keeping all lower_cased
would be a simple solution for all. @sbillinge Do you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, one last thing on the news.
news/colorupdate.rst
Outdated
@@ -4,7 +4,6 @@ | |||
|
|||
**Changed:** | |||
|
|||
* Change of two colors for higher contrast: ``#AB987A`` to ``#918770`` and ``#984B43`` to ``#6C5050`` | |||
* Capitalized the hex color codes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be under fixed. Changed refers to changes that would affect the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be under fixed. Changed refers to changes that would affect the user.
Noted. Moved and please review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. one comment again...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please review!
It looks like the problem is a lower case b in |
!! @sbillinge thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few comments that could help the usability
src/bg_mpl_stylesheets/colors.py
Outdated
for color in cls: | ||
if color.value.lower() == hex_value.lower(): | ||
return color.name | ||
raise ValueError("Unknown color name. Please check Colors enum for available colors.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a non programmer user know how to do this? I think this is too jargony to be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
raise ValueError("You have entered an unknown color hex value."
" Please check the src/bg_mpl_stylesheets/colors.py file for the available colors.")
You have entered an unknown color hex value -> reason for the error
Please check the src/bg_mpl_stylesheets/colors.py file for the available colors -> what to do fix to it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, much better error message. Even better would be a method that lists the available colors maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, we already have that method, so could we mention that in the error message as an option? Look in the file or run that method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a few more thoughts to make this more usable? Not sure what you think.
src/bg_mpl_stylesheets/colors.py
Outdated
# Add more colors as needed | ||
|
||
@classmethod | ||
def get_color_name(cls, hex_value: str) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually need these. It seems that we are not making the best use of enums. For example, for usage, wouldn't something like this be cleaner?:
import bg_mpl_stylesheets.colors.Colors as bgcolors
for color in bg.colors:
ax.plot(x, y + offset * i, label=color.name, color=color.value, linestyle="-")
and if we want to address explicitly, something like
bgblue = bgcolors.bg_blue
ax.plot(x, y + offset * i, label=bgblue.name, color=bgblue.value, linestyle="-")
I see that we would need the hex to name converter when using the cycler as a cycle unless we could figure out how to make the cycler elements the enum values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class Colors(str, Enum):
bg_blue = "#0B3C5D"
bg_red = "#B82601"
bg_green = "#1C6B0A"
bg_light_blue = "#328CC1"
bg_light_grey = "#A8B6C1"
bg_yellow = "#D9B310"
bg_brown = "#6C5050"
bg_burgundy = "#76323F"
bg_olive_green = "#626E60"
bg_muted_olive = "#918770"
bg_beige = "#C09F80"
bg_grey = "#B0B0B0FF"
Right, this would be it based on your suggestion above.
The purpose of get_color_name
was for testing below and changing the legends from hex to color names:
In the example code under example/color_cycles.py
, we are testing the hard coded bg-style
.
![Screenshot 2024-08-21 at 4 49 11 PM](https://private-user-images.githubusercontent.com/14892262/360093553-7086c3f7-eb84-4df3-b50a-373acd880d13.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzNjY1NjAsIm5iZiI6MTczOTM2NjI2MCwicGF0aCI6Ii8xNDg5MjI2Mi8zNjAwOTM1NTMtNzA4NmMzZjctZWI4NC00ZGYzLWI1MGEtMzczYWNkODgwZDEzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDEzMTc0MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk4MDAyZTQzNTA3MjdhNDJjOTFhOThkN2QwZGQyZDgyMjc4N2VhYmI2YzBjNGE2MDNiZWI2YTE3NGNmMzQxNTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.wrottcgvfFB9J4qyUGG2W_KUXOLxKxQSFhBV0-r_gDI)
cycle = plt.rcParams["axes.prop_cycle"].by_key()["color"]
# plot the color cycles and corresponding color codes.
for i, hex in enumerate(cycle):
color_name = Colors.get_color_name(hex)
ax.plot(x, y + offset * i, label=color_name, color=hex, linestyle="-")
If do not have the get_color_name
class method, then we do not have access to the color name since the hex
a string.
If retrieving the color name from a string is not needed (legends for the the example code), then, we can remove get_color_name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no way to get that figure as it is but without the get_color_name
method but using the enum properties? That may be true, but something like replacing the hex hard code with the enum values could fix it? It is still "immutable" in the way that someone could extend the colors enum and it wouldn't change the cycler (which is what we seek for the immutability). We could also test it with hard coded hex's so if someone changes the hex of one of the used enums the test would break.
I will address the comments later in the afternoon and see how we can make this package useful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please see comments. I have to hop on a flight. I will look at more later.
README.rst
Outdated
bg_blue_hex = Colors.bg_blue.value | ||
|
||
# Get color name | ||
bg_blue_name = Colors.bg_blue.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here we are teaching people somewhat ugly coding practices so I would remove these bg_blue_hex
etc. variables.
We do want to let people know how to access the name and hex values, but let's do that with words, not with bad code....
The beauty of the enum is how easy and pythonic
it is to access the names and values.
I am not totally sure why we don't use a dict, but it is ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup value and name directly accessed in the example.
Safe flight. I'll give this some thought and address them in the afternoon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbillinge Please review!
ax.plot(x, y + offset * i, label="\\{}".format(c), color=c, linestyle="-") | ||
|
||
for i, hex in enumerate(cycle): | ||
ax.plot(x, y + offset * i, label=Colors(hex).name, color=hex, linestyle="-") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no way to get that figure as it is but without the
get_color_name
method but using the enum properties? That may be true, but something like replacing the hex hard code with the enum values could fix it? It is still "immutable" in the way that someone could extend the colors enum and it wouldn't change the cycler (which is what we seek for the immutability). We could also test it with hard coded hex's so if someone changes the hex of one of the used enums the test would break.
@sbillinge You are correct.
I found out that we do not need get_color_name
. Python's enum provides Colors(hex).name
where we can get the property variable name from a string. Didn't know about this feature but certainly saved many lines of code.
Lovely @bobleesj, Thanks so much! |
Closes #40
Motivation
We want the user to choose a color.
We do not want the user to memorize the color name.
Proposed solution
@sbillinge If you find this implementation useful, I will update README.md