Skip to content

Adding background_color #35

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

Merged
merged 13 commits into from
Feb 18, 2020
Merged

Adding background_color #35

merged 13 commits into from
Feb 18, 2020

Conversation

FoamyGuy
Copy link
Contributor

This change allows you to set background_color of the label in the constructor or with a property. Setting background_color to false, or excluding it entirely will result in same current behavior which is transparent background. This is referenced in issue #33.

@FoamyGuy FoamyGuy requested a review from a team February 15, 2020 00:44
@FoamyGuy FoamyGuy linked an issue Feb 15, 2020 that may be closed by this pull request
@FoamyGuy FoamyGuy requested a review from a team February 17, 2020 15:02
@makermelissa
Copy link
Collaborator

Hi, looks mostly good. However, if you don't set the background color so that it is transparent and read the property, it returns 0, which is the same value returned if it is black. Can we modify this so that if the color is transparent, it returns None?

It doesn't appear circuitpython currently has a native way to check whether it is transparent, so we may need to store that as a flag on the label internally.

@@ -57,7 +57,7 @@ class Label(displayio.Group):
:param int color: Color of all text in RGB hex
:param double line_spacing: Line spacing of text to display"""
def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff,
line_spacing=1.25, **kwargs):
background_color=False, line_spacing=1.25, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the default to None instead and then change your logic to match? You can check for None easily:

if background_color:
    # action if there is a color set
else:
    # action if it's None

text_area = label.Label(terminalio.FONT, text=text, color=0x0000FF, background_color=0xFFAA00)
text_area.x = 10
text_area.y = 10
board.DISPLAY.show(text_area)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add some lines that show:

  • querying current background color
  • changing back to no background color

@property
def background_color(self):
"""Color of the background as an RGB hex number."""
return self.palette[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if this would return None when there is no background color set.

@FoamyGuy
Copy link
Contributor Author

@makermelissa @caternuson I believe all of the mentioned changes have been made with the new commits. In making and testing the changes I also found and fixed a few issues with changing too and from transparent background colors.

@makermelissa
Copy link
Collaborator

Thanks @FoamyGuy. One more small thing. Can we change it so self.transparent_background is private so that it becomes self._transparent_background. That way you don't have users doing something silly like:

text_area = label.Label(terminalio.FONT, text="hello", color=0x0000FF)
text_area.transparent_background = False
print(text_area.background_color)

@FoamyGuy
Copy link
Contributor Author

@makermelissa Got it done. Thank you.

self.palette.make_transparent(0)
else:
self.transparent_background = False
self.palette[0] = background_color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I totally forgot about needing to accept 0 as a possible valid color. If that were done with above, then it would be treated as setting transparent. How about this?

        if background_color is not None:
            self.palette[0] = background_color
            self.palette.make_opaque(0)
            self.transparent_background = False
        else:
            self.palette.make_transparent(0)
            self.transparent_background = True

Or could maybe do like you did in the setter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but also with the leading underscore)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, thank you. just pushed some commits to fix this in the constructor.

@@ -71,7 +71,14 @@ def __init__(self, font, *, x=0, y=0, text=None, max_glyphs=None, color=0xffffff
self.y = y

self.palette = displayio.Palette(2)
self.palette.make_transparent(0)
if background_color or background_color == 0x000000:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better expressed as: if background_color is not None:


@background_color.setter
def background_color(self, new_color):
if new_color or new_color == 0x000000:
Copy link
Collaborator

@makermelissa makermelissa Feb 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with this: if new_color is not None:

@makermelissa
Copy link
Collaborator

Thank you for making these changes. We want your code to be in its best possible form.

@FoamyGuy
Copy link
Contributor Author

@makermelissa there is a new commit now that changes both if statements to use the is not None syntax. I don't foresee any issues with it, but I won't be able to test the new commit specifically on a device until later tonight.

I really appreciate everyone taking the time to point out ways it can be improved. I cut my teeth as a solo programmer, and even now I work on a team of only 2-3 developers. So feedback on source code is something I haven't got much experience with before getting involved with Circuit Python. I appreciate the feedback because I can hopefully learn and improve from it.

@caternuson
Copy link
Contributor

@FoamyGuy Thanks for making all the changes.

@makermelissa At this point - looks OK to me.

Copy link
Collaborator

@makermelissa makermelissa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me too. Thanks @FoamyGuy!

@makermelissa makermelissa merged commit b506a0a into adafruit:master Feb 18, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 19, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_BusDevice to 4.2.1 from 4.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_BusDevice#45 from dhalbert/fix-end-values

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Text to 2.4.0 from 2.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Text#35 from FoamyGuy/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyBadger to 1.1.1 from 1.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyBadger#20 from nnja/fix_business_card_display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Label could have a background color
3 participants