Skip to content

Changed color, stroke_color and fill_color attributes to properties #2332

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

Conversation

marcin-serwin
Copy link
Collaborator

@marcin-serwin marcin-serwin commented Nov 26, 2021

Overview: What does this pull request change?

  • color, stroke_color and fill_color are now properties in VMobject, making them synchronized with set_* and get_* methods
  • Surrounding code depending on these attributes was also updated to work properly with them

Motivation and Explanation: Why and how do your changes improve the library?

Fixes #2324.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@hydrobeam hydrobeam added the pr:bugfix Bug fix for use in PRs solving a specific issue:bug label Nov 27, 2021
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

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

I'm slightly hesitant to merge this – on the one hand, this is a nice quality of life improvement for Mobject, but at the same time it lets VMobject diverge more from OpenGLVMobject. Maybe the same attributes could be turned into properties for OpenGLVMobject as well?

@marcin-serwin marcin-serwin marked this pull request as draft December 3, 2021 14:34
@marcin-serwin marcin-serwin force-pushed the change-colors-to-properties branch from 9ddaf5f to eaf7f69 Compare December 3, 2021 15:06
@marcin-serwin marcin-serwin marked this pull request as ready for review December 3, 2021 20:03
@marcin-serwin marcin-serwin requested a review from behackl December 3, 2021 20:05
Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

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

This looks good now, thank you! It would be good to have someone else look over your changes as well; maybe @hydrobeam?

@marcin-serwin marcin-serwin force-pushed the change-colors-to-properties branch from b958734 to 631f7ad Compare December 22, 2021 21:22
Copy link
Member

@hydrobeam hydrobeam left a comment

Choose a reason for hiding this comment

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

Left some questions about some decisions here, also some minor comments.

@@ -78,6 +78,7 @@ class Mobject:
"""

animation_overrides = {}
submobjects = []
Copy link
Member

Choose a reason for hiding this comment

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

What was the reasoning for adding this class level attribute? Also, why did you decide against this in opengl_mobject.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was an ugly workaround similar to the one pointed out in the previous review. I've removed it.


self.color = Color(color) if color else None

Copy link
Member

Choose a reason for hiding this comment

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

NIT: odd spacing

Suggested change
self.color = Color(color) if color else None
self.color = Color(color) if color else None

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

@@ -76,6 +77,10 @@ def __init__(
should_subdivide_sharp_curves=False,
should_remove_null_curves=False,
color=None,
*,
Copy link
Member

Choose a reason for hiding this comment

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

Is this a typo or intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is intentional, so as not to change the constructor signature. fill_color, stroke_color and stroke_opacity were keyword only before and they are keyword only now.

@@ -1066,7 +1065,7 @@ def __init__(
else:
self.line_spacing = self._font_size + self._font_size * self.line_spacing

file_name = self.text2svg()
file_name = self.text2svg(Color(color) if color else None)
Copy link
Member

Choose a reason for hiding this comment

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

What happens if None is passed here? I'm not familiar with manimpango so I'm not sure if None could cause problems here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since None is not a valid color it would default to black, but I've made it more explicit.

self.update_rgbas_array(array_name, color, opacity)
if width is not None:
setattr(self, width_name, width)
if opacity is not None:
setattr(self, opacity_name, opacity)
if color is not None:
if background:
Copy link
Member

Choose a reason for hiding this comment

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

Why switch this to depending on background? Also, it might be better to have it lumped in with the rest of the if background: attribute setting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The old condition, color is not None, should also be present, I've fixed it now. It is depending on background because only then the color_name should be updated, otherwise it does not exist. I've made it more explicit, so hopefully the code is more understandable now.

@@ -56,10 +56,10 @@ def __set_name__(self, obj, name):
self.name = name

def __get__(self, obj, owner):
return obj.__dict__["data"][self.name]
return obj.data[self.name] if self.name in obj.data else []
Copy link
Member

Choose a reason for hiding this comment

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

Checking if an attribute is in data might prove to be too expensive and cause a performance dropoff, since data is called a lot of times (haven't tested, just speculation). Is there a way to get around doing this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This turned out to be no longer necessary after fixing ugly workarounds described above.

self.stroke_opacity = stroke_opacity
self.stroke_width = stroke_width
self.stroke_width = [stroke_width]
Copy link
Member

Choose a reason for hiding this comment

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

Why did this need to be changed? Iirc set_stroke should be handling converting the float into a 2d-array to be placed into self.data

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've no idea why I've changed it. I've removed it and everything works fine.

@Darylgolden Darylgolden requested a review from hydrobeam January 14, 2022 01:54
@Darylgolden
Copy link
Member

Sorry for the long wait! Can you resolve the merge conflicts?

@marcin-serwin marcin-serwin force-pushed the change-colors-to-properties branch from 5d15936 to aeca1fa Compare January 28, 2022 10:39
Copy link
Member

@Darylgolden Darylgolden left a comment

Choose a reason for hiding this comment

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

Left two minor comments about typings, otherwise LGTM!

@@ -9,11 +9,12 @@
import re
import string
import warnings
from typing import Dict, List
from typing import Dict, List, Optional
Copy link
Member

Choose a reason for hiding this comment

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

These imports are not used.

@@ -1225,7 +1225,7 @@ def _text2hash(self):
hasher.update(id_str.encode())
return hasher.hexdigest()[:16]

def _text2svg(self):
def _text2svg(self, color: Optional[Color]):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def _text2svg(self, color: Optional[Color]):
def _text2svg(self, color: Color | None):

@Darylgolden Darylgolden merged commit 717abfb into ManimCommunity:main Jan 28, 2022
@naveen521kk
Copy link
Member

@marcin-serwin
Copy link
Collaborator Author

Looks like this has broken t2s and t2w in Text https://docs.manim.community/en/latest/reference/manim.mobject.svg.text_mobject.Text.html#textitalicandboldexample

I'll look at it tomorrow.

@behackl behackl changed the title Change color, stroke_color and fill_color fields to properties Changed color, stroke_color and fill_color attributes to properties Feb 20, 2022
@behackl behackl added the enhancement Additions and improvements in general label Feb 20, 2022
@behackl behackl changed the title Changed color, stroke_color and fill_color attributes to properties Changed color, stroke_color and fill_color attributes to properties Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Additions and improvements in general pr:bugfix Bug fix for use in PRs solving a specific issue:bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Attribute not updated when animated with .animate
5 participants