Skip to content

Conversation

@Enegg
Copy link
Contributor

@Enegg Enegg commented Dec 1, 2025

Summary

This PR adds a new factory method .from_hex to disnake.Colour which accepts strings in the form of "#RRGGBB".
This is particularly useful in code editors that provide a color picker when they detect a color code.
VSCode displaying colored squares next to color codes

Checklist

  • If code changes were made, then they have been tested
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running uv run nox -s lint
    • I have type-checked the code by running uv run nox -s pyright
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@shiftinv
Copy link
Member

shiftinv commented Dec 2, 2025

ext.commands.ColourConverter also handles hex colors of various formats, it could probably make use of this new method too.

Co-authored-by: Zenith <[email protected]>
Signed-off-by: Eneg <[email protected]>
@Enegg
Copy link
Contributor Author

Enegg commented Dec 4, 2025

ext.commands.ColourConverter also handles hex colors of various formats, it could probably make use of this new method too.

I did expect there being a converter for this, but I'm not convinced this simple method is going to be of much use to it.

I can see these two relevant methods:

def parse_hex_number(self, argument: str) -> disnake.Color:
arg = "".join(i * 2 for i in argument) if len(argument) == 3 else argument
try:
value = int(arg, base=16)
if not (0 <= value <= 0xFFFFFF):
raise BadColourArgument(argument)
except ValueError:
raise BadColourArgument(argument) from None
else:
return disnake.Color(value=value)

async def convert(self, ctx: AnyContext, argument: str) -> disnake.Color:
if argument[0] == "#":
return self.parse_hex_number(argument[1:])
if argument[0:2] == "0x":
rest = argument[2:]
# Legacy backwards compatible syntax
if rest.startswith("#"):
return self.parse_hex_number(rest[1:])
return self.parse_hex_number(rest)
arg = argument.lower()
if arg[0:3] == "rgb":
return self.parse_rgb(arg)
arg = arg.replace(" ", "_")
method = getattr(disnake.Colour, arg, None)
if arg.startswith("from_") or method is None or not inspect.ismethod(method):
raise BadColourArgument(arg)
return method()

...but they perform work/validation that goes beyond simply stripping the leading # and parsing as base 16. It also seems to support 3-digit #RGB format.

@shiftinv shiftinv added the t: enhancement New feature label Dec 12, 2025
@shiftinv shiftinv added this to disnake Dec 12, 2025
@github-project-automation github-project-automation bot moved this to In Progress in disnake Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t: enhancement New feature

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants