Skip to content
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

Fix Color::get_{r,g,b,a}8 #47726

Merged
merged 1 commit into from
Apr 16, 2021
Merged

Fix Color::get_{r,g,b,a}8 #47726

merged 1 commit into from
Apr 16, 2021

Conversation

dalexeev
Copy link
Member

@dalexeev dalexeev commented Apr 8, 2021

Closes #47022.

@dalexeev dalexeev requested a review from a team as a code owner April 8, 2021 14:43
@Chaosus Chaosus added this to the 4.0 milestone Apr 8, 2021
Copy link
Member

@aaronfranke aaronfranke left a comment

Choose a reason for hiding this comment

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

This is good, but while we're changing these lines, this can be better. Color always uses 32-bit floats, so these should have f as a suffix (or we could also do 255 without the .0 for the same behavior, but 255.0f makes our intentions clear):

	_FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0); }
	_FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(Math::round(r * 255.0f), 0.0f, 255.0f)); }
	_FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0); }
	_FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(Math::round(g * 255.0f), 0.0f, 255.0f)); }
	_FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0); }
	_FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(Math::round(b * 255.0f), 0.0f, 255.0f)); }
	_FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0); }
	_FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(Math::round(a * 255.0f), 0.0f, 255.0f)); }

@dalexeev
Copy link
Member Author

dalexeev commented Apr 8, 2021

Can we do this:

_FORCE_INLINE_ int32_t get_r8() const { return CLAMP(int32_t(Math::round(r * 255.0f)), 0, 255); }

?

@aaronfranke
Copy link
Member

The core devs did the opposite in this commit, so I don't think we should undo it: 771b3c5#diff-338a6122d06ba451b648508685a84917a457cf904b0aa28e736c62fe4dd950b6R197

@aaronfranke aaronfranke requested a review from akien-mga April 9, 2021 13:23
@akien-mga akien-mga merged commit a3ca784 into godotengine:master Apr 16, 2021
@akien-mga
Copy link
Member

Thanks!

@akien-mga akien-mga added the cherrypick:3.x Considered for cherry-picking into a future 3.x release label Apr 16, 2021
@dalexeev dalexeev deleted the patch-1 branch April 16, 2021 08:32
@akien-mga
Copy link
Member

In #47022 you state that it affects 3.2.4-rc4 too, but the code seems different and does use round AFAICT:

godot/core/variant_op.cpp

Lines 1664 to 1671 in 137f71f

} else if (p_index == CoreStringNames::singleton->r8) {
return int(Math::round(v->r * 255.0));
} else if (p_index == CoreStringNames::singleton->g8) {
return int(Math::round(v->g * 255.0));
} else if (p_index == CoreStringNames::singleton->b8) {
return int(Math::round(v->b * 255.0));
} else if (p_index == CoreStringNames::singleton->a8) {
return int(Math::round(v->a * 255.0));

godot/core/variant_op.cpp

Lines 2507 to 2518 in 137f71f

} else if (*str == "r8") {
valid = true;
return (int)Math::round(v->r * 255.0);
} else if (*str == "g8") {
valid = true;
return (int)Math::round(v->g * 255.0);
} else if (*str == "b8") {
valid = true;
return (int)Math::round(v->b * 255.0);
} else if (*str == "a8") {
valid = true;
return (int)Math::round(v->a * 255.0);

If the issue is still reproducible in 3.x, it likely needs a different fix.

@akien-mga akien-mga removed the cherrypick:3.x Considered for cherry-picking into a future 3.x release label Apr 26, 2021
@dalexeev
Copy link
Member Author

@akien-mga The problem is reproduced in 3.3. But it seems that the reason is really something else. I can't figure out what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Color::get_{r,g,b,a}8() use truncate instead of round
4 participants