+
Skip to content

Support ColorResolvable via "color" ArgumentType. #81

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Noxillio
Copy link
Contributor

@Noxillio Noxillio commented Oct 3, 2019

It's a little messy, so I welcome anyone to improve it, but this adjustment allows common names, hex values (with and without #), 'RANDOM', RGB array, and numbers to be used as values for the 'color' ArgumentType.

Note that when using an RGB array, either the array must not have spaces in it, or quoted: true must be applied to the argument.

This method takes advantage of the resolveColor() method from discord.js. This theoretically means that anything that can be considered ColorResolvable should work. I've tested it with hex with and without #, RGB array with and without spaces/quotes, numbers, common names, and RANDOM, as listed here in the Discord.js documentation.

This method also will not break the existing method which is to simply use a hex code as the value of an argument with type: 'color'.

It's a little messy, so I welcome anyone to improve it, but this adjustment allows common names, hex values (with and without #), RANDOM, RGB array, and numbers to be used as values for the color ArgumentType.

Note that when using an RGB array, either the array must not have spaces in it, or `quoted: true` must be applied to the argument.

This method takes advantage of the resolveColor() method from discord.js. This theoretically means that anything that can be considered ColorResolvable should work.
Copy link
Contributor

@GamesProSeif GamesProSeif left a comment

Choose a reason for hiding this comment

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

Review

This change is great since it can now parse HEX values, RGB values, and CSS keywords.
I have no problem with the current way it parses but I feel spaces need to be replaced with underscores in order for the resolveColor function to work properly if you supply it with a CSS keyword.


Regex Matching

If anyone feels this would perform better with regex, I've made one regex pattern that matches the three colour types.

/^(?:(?<hex>#[0-9A-F]{6})|\[(?<r>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]),(?<g>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]),(?<b>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\]|(?<string>[A-Z_]{1,19}))$/i

Here's a link if you want to test it. Although it might need a small change for matching CSS keywords, since it currently matches the pattern strictly to have underscores instead of spaces, and have a maximum length of 19, as the longest CSS keyword that discord.js can parse is LUMINOUS_VIVID_PINK, which is 19 characters long.
It has named groups, so you know which type is detected.

  • hex - for hexadecimal value matches
  • r, g, b - for RGB value matches (those are 3 groups not one)
  • string - for CSS keyword matches.

Note: string currently matches any word, and is not limited to CSS keywords strictly.


To sum up, this change will open a lot of chances for matching colors as a built-in type. More helpful for programmers who have no idea of TypeResolver#addType. Looking forward to this PR getting merged!

@Noxillio
Copy link
Contributor Author

Noxillio commented Oct 3, 2019

Review

This change is great since it can now parse HEX values, RGB values, and CSS keywords.
I have no problem with the current way it parses but I feel spaces need to be replaced with underscores in order for the resolveColor function to work properly if you supply it with a CSS keyword.

Regex Matching

If anyone feels this would perform better with regex, I've made one regex pattern that matches the three colour types.

/^(?:(?<hex>#[0-9A-F]{6})|\[(?<r>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]),(?<g>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]),(?<b>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\]|(?<string>[A-Z_]{1,19}))$/i

Here's a link if you want to test it. Although it might need a small change for matching CSS keywords, since it currently matches the pattern strictly to have underscores instead of spaces, and have a maximum length of 19, as the longest CSS keyword that discord.js can parse is LUMINOUS_VIVID_PINK, which is 19 characters long.
It has named groups, so you know which type is detected.

  • hex - for hexadecimal value matches
  • r, g, b - for RGB value matches (those are 3 groups not one)
  • string - for CSS keyword matches.

Note: string currently matches any word, and is not limited to CSS keywords strictly.

To sum up, this change will open a lot of chances for matching colors as a built-in type. More helpful for programmers who have no idea of TypeResolver#addType. Looking forward to this PR getting merged!

I never knew CSS keywords were supported. If that's really true, it could certainly be accounted for.

@GamesProSeif
Copy link
Contributor

@Noxillio
Copy link
Contributor Author

Noxillio commented Oct 3, 2019

Oh, those. I'm still not quite sure what you're talking about, because underscores work just fine with this as seen in the screenshot.
image

@GamesProSeif
Copy link
Contributor

resolveColor doesn't work normally if you add spaces
This is what I mean, notice the difference.

const { resolveColor } = require('discord.js');

console.log(resolveColor('DARK_RED')); // 10038562
console.log(resolveColor('DARK RED')); // 218

@Noxillio
Copy link
Contributor Author

Noxillio commented Oct 3, 2019

I think I understand now. I'll do some tinkering.

Copy link
Contributor

@GamesProSeif GamesProSeif left a comment

Choose a reason for hiding this comment

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

Yup, that's good for now!

@Noxillio
Copy link
Contributor Author

Noxillio commented Oct 3, 2019

Very simple fix. Just had to add .replace(' ', '_').

Copy link
Contributor

@papaia papaia left a comment

Choose a reason for hiding this comment

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

I believe destructuring from Util is clearer.

@Noxillio
Copy link
Contributor Author

Finally, jfc...

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.

4 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载