-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
background_color
Transparency Fixes
#2484
base: main
Are you sure you want to change the base?
Changes from 17 commits
1e75114
1e075fa
ca50f58
6c1aeea
24abf08
a94f0de
c30aa56
6fe78dd
4a242dd
054e07b
6b1bf9e
4b5ed6f
77df2e9
6981a7a
b2ca091
1b3122c
c767a5b
9921d79
06d40e1
9254e18
e53879d
d43aba4
059578f
298d460
c6382c3
e5e3e07
ca4d5d9
8c62422
b0c8613
b6cf7d5
4b7991d
9e354f8
71f461b
b2f73a7
a0e141f
350b9d0
aad928c
9e11129
4aaedd4
a1cf579
2bb6a39
c9c714e
553ac6e
2f657db
f63859c
3e0f883
6f75e5f
a88cc13
221fd10
dbe7b42
3054df1
135741f
5991cd2
165d4b9
b2d0c47
a077b0e
d2bfbfb
544802c
db05029
f7687a8
4e4a82e
0ff7752
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,13 @@ def reference_variant(self, reference): | |
def get_image(self): | ||
return Image.open(BytesIO(self.impl.get_image_data())) | ||
|
||
def test_get_image_data_internal_fail(self, monkeypatch): | ||
original_background = self.native.getBackground() | ||
self.native.setBackground(None) | ||
with pytest.warns(match="Failed to get canvas background"): | ||
self.impl.get_image_data() | ||
self.native.setBackground(original_background) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the comment on the implementation - why is this occurring? Why is the test suite passing with 100% branch coverage prior to this change? |
||
|
||
def motion_event(self, action, x, y): | ||
time = SystemClock.uptimeMillis() | ||
super().motion_event( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably needs to be broken up into a couple of release notes - one for each underlying issue that we're resolving. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Bugs related to transparent background color are fixed. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,64 @@ | ||
from System.Drawing import Color | ||
from travertino.colors import TRANSPARENT | ||
from travertino.colors import TRANSPARENT, rgb, rgba | ||
|
||
CACHE = {TRANSPARENT: Color.Transparent} | ||
|
||
|
||
def native_color(c): | ||
def native_color(toga_color): | ||
try: | ||
color = CACHE[c] | ||
color = CACHE[toga_color] | ||
except KeyError: | ||
color = Color.FromArgb( | ||
int(c.rgba.a * 255), int(c.rgba.r), int(c.rgba.g), int(c.rgba.b) | ||
int(toga_color.rgba.a * 255), | ||
int(toga_color.rgba.r), | ||
int(toga_color.rgba.g), | ||
int(toga_color.rgba.b), | ||
) | ||
CACHE[c] = color | ||
CACHE[toga_color] = color | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return color | ||
|
||
|
||
def toga_color(native_color): | ||
return rgba(native_color.R, native_color.G, native_color.B, native_color.A / 255) | ||
|
||
|
||
def alpha_blending_over_operation(child_color, parent_color): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of I'd also be inclined to add this as a utility in core (or possibly even in Travertino). Although we're not actually using this anywhere other than Winforms, the generic ability to do alpha blending isn't a bad thing to have, and it would allow us to explicitly test the blending API with core tests, rather than implicitly testing it through usage in Winforms. |
||
# The blending operation I have implemented here is the "over" operation and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code comments generally shouldn't refer to the author. We're describing the current state of the code, which isn't dependent on any one author. |
||
# replicates CSS's rgba mechanism. For the formula used here, see: | ||
# https://en.wikipedia.org/wiki/Alpha_compositing#Description | ||
|
||
blended_alpha = child_color.a + ((1 - child_color.a) * parent_color.a) | ||
|
||
# Check if the blended alpha is zero, indicating no blending | ||
if blended_alpha == 0: | ||
# If both child and parent alphas are 0, no blending occurs, so return child color. | ||
return child_color | ||
|
||
blended_color = rgb( | ||
# Red Component | ||
( | ||
( | ||
(child_color.r * child_color.a) | ||
+ (parent_color.r * parent_color.a * (1 - child_color.a)) | ||
) | ||
/ blended_alpha | ||
), | ||
# Green Component | ||
( | ||
( | ||
(child_color.g * child_color.a) | ||
+ (parent_color.g * parent_color.a * (1 - child_color.a)) | ||
) | ||
/ blended_alpha | ||
), | ||
# Blue Component | ||
( | ||
( | ||
(child_color.b * child_color.a) | ||
+ (parent_color.b * parent_color.a * (1 - child_color.a)) | ||
) | ||
/ blended_alpha | ||
), | ||
) | ||
return blended_color |
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 doesn't make any sense to me. Why couldn't the background be obtained?