-
Notifications
You must be signed in to change notification settings - Fork 360
Updated the color setter so that it does not change the current alpha #1970
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2b3774
Updated the color setter so that it does not change the current alpha
FriendlyGecko 11efaab
Updated it to check for use arcade.Color
FriendlyGecko 6a1d2b4
Demo 1
FriendlyGecko d8ae39b
Added deeper RGB class
FriendlyGecko 29c14b0
cleaned up RGB
FriendlyGecko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from array import array | ||
| import ctypes | ||
| import random | ||
|
|
@@ -20,8 +19,7 @@ | |
| Sequence, | ||
| Tuple, | ||
| Union, | ||
| TYPE_CHECKING, | ||
| TypeVar | ||
| TYPE_CHECKING, TypeVar | ||
| ) | ||
| from typing_extensions import Self | ||
|
|
||
|
|
@@ -75,6 +73,92 @@ | |
| ] | ||
|
|
||
|
|
||
| class RGB(Tuple): | ||
| """ | ||
| A :py:class:`tuple` subclass representing an RGB color. | ||
|
|
||
| All channels are byte values from 0 to 255, inclusive. If any are | ||
| outside this range, a :py:class:`~arcade.utils.ByteRangeError` will | ||
| be raised, which can be handled as a :py:class:`ValueError`. | ||
|
|
||
| :param r: the red channel of the color, between 0 and 255 | ||
| :param g: the green channel of the color, between 0 and 255 | ||
| :param b: the blue channel of the color, between 0 and 255 | ||
| """ | ||
|
|
||
| def __new__(cls, r: int, g: int, b: int): | ||
|
|
||
| if not 0 <= r <= 255: | ||
| raise ByteRangeError("r", r) | ||
|
|
||
| if not 0 <= g <= 255: | ||
| raise ByteRangeError("g", g) | ||
|
|
||
| if not 0 <= g <= 255: | ||
| raise ByteRangeError("b", b) | ||
|
|
||
| # Typechecking is ignored because of a mypy bug involving | ||
| # tuples & super: | ||
| # https://github.com/python/mypy/issues/8541 | ||
| return super().__new__(cls, (r, g, b)) # type: ignore | ||
|
|
||
| def __deepcopy__(self, _) -> Self: | ||
| """Allow :py:func:`~copy.deepcopy` to be used with Color""" | ||
| return self.__class__(r=self.r, g=self.g, b=self.b) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"{self.__class__.__name__}(r={self.r}, g={self.g}, b={self.b})" | ||
|
|
||
| @property | ||
| def r(self) -> int: | ||
| return self[0] | ||
|
|
||
| @property | ||
| def g(self) -> int: | ||
| return self[1] | ||
|
|
||
| @property | ||
| def b(self) -> int: | ||
| return self[2] | ||
|
|
||
| @classmethod | ||
| def random( | ||
| cls, | ||
| r: Optional[int] = None, | ||
| g: Optional[int] = None, | ||
| b: Optional[int] = None, | ||
| ) -> Self: | ||
| """ | ||
| Return a random color. | ||
|
|
||
| The parameters are optional and can be used to fix the value of | ||
| a particular channel. If a channel is not fixed, it will be | ||
| randomly generated. | ||
|
|
||
| Examples:: | ||
|
|
||
| # Randomize all channels | ||
| >>> Color.random() | ||
| Color(r=35, g=145, b=4, a=200) | ||
|
|
||
| # Random color with fixed alpha | ||
| >>> Color.random(a=255) | ||
| Color(r=25, g=99, b=234, a=255) | ||
|
|
||
| :param r: Fixed value for red channel | ||
| :param g: Fixed value for green channel | ||
| :param b: Fixed value for blue channel | ||
| """ | ||
| if r is None: | ||
| r = random.randint(0, 255) | ||
| if g is None: | ||
| g = random.randint(0, 255) | ||
| if b is None: | ||
| b = random.randint(0, 255) | ||
|
|
||
| return cls(r, g, b) | ||
|
|
||
|
|
||
| class Color(RGBA255): | ||
| """ | ||
| A :py:class:`tuple` subclass representing an RGBA Color. | ||
|
|
@@ -450,11 +534,11 @@ class TiledObject(NamedTuple): | |
| type: Optional[str] = None | ||
|
|
||
|
|
||
| if sys.version_info >= (3, 12): | ||
|
Member
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. Why remove this? My concerns:
Contributor
Author
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 wasn't intentional, I still have to clean it up before it is ready. |
||
| from collections.abc import Buffer as BufferProtocol | ||
| else: | ||
| # This is used instead of the typing_extensions version since they | ||
| # use an ABC which registers virtual subclasses. This will not work | ||
| # with ctypes.Array since virtual subclasses must be concrete. | ||
| # See: https://peps.python.org/pep-0688/ | ||
| BufferProtocol = Union[ByteString, memoryview, array, ctypes.Array] | ||
| # This is a temporary workaround for the lack of a way to type annotate | ||
| # objects implementing the buffer protocol. Although there is a PEP to | ||
| # add typing, it is scheduled for 3.12. Since that is years away from | ||
| # being our minimum Python version, we have to use a workaround. See | ||
| # the PEP and Python doc for more information: | ||
| # https://peps.python.org/pep-0688/ | ||
| # https://docs.python.org/3/c-api/buffer.html | ||
| BufferProtocol = Union[ByteString, memoryview, array, ctypes.Array] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is cleaner, but it's worth doing benchmarks to see if it's slower. The reason pyglet code and the
from_iterablemethod use the strange-looking unpacking method is that it's faster.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'm not sure I understand all the changes intended or mentioned."
The intent is to make it so RGB and alpha are used together to create color and by default not changing the alpha value of a sprite unless specifically called on.
The idea being that RGB can more easily be manipulated separately.