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

feat: color conversion utilities for (hsv, rgb, and hex) #200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

meowgorithm
Copy link
Member

This adds functions for converting to and from HSV.

There's also a color to hexadecimal conversion for convenience.

@caarlos0
Copy link
Member

missing run

./scripts/dependabot
./scripts/builds

to setup dependabot and GitHub actions

Copy link
Member

@aymanbagabas aymanbagabas left a comment

Choose a reason for hiding this comment

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

LGTM. I'm curious, why not use go-colorful?


// ColorToHex converts a color to a hex string.
func ColorToHex(c color.Color) string {
r, g, b, _ := c.RGBA()
Copy link
Member

Choose a reason for hiding this comment

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

Needs a nil check

Suggested change
r, g, b, _ := c.RGBA()
if c == nil {
return ""
}
r, g, b, _ := c.RGBA()

// - The hue will be rounded to the nearest degree, while the saturation and
// value will be rounded to two decimal places.
func ColorToHSV(c color.Color) (h, s, v float64) {
r, g, b, _ := c.RGBA()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
r, g, b, _ := c.RGBA()
if c == nil {
return 0, 0, 0
}
r, g, b, _ := c.RGBA()

c color.Color
want string
}{
{color.RGBA{0, 0, 0, 255}, "#000000"},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{color.RGBA{0, 0, 0, 255}, "#000000"},
{nil, ""},
{color.RGBA{0, 0, 0, 255}, "#000000"},

h, s, v float64
}
}{
{color.RGBA{0, 0, 0, 255}, struct{ h, s, v float64 }{0, 0, 0}},
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{color.RGBA{0, 0, 0, 255}, struct{ h, s, v float64 }{0, 0, 0}},
{nil, struct{ h, s, v float64 }{0, 0, 0}},
{color.RGBA{0, 0, 0, 255}, struct{ h, s, v float64 }{0, 0, 0}},

@meowgorithm
Copy link
Member Author

Appreciate the review. Yeah, after putting this together it occurred to me that go-colorful is probably a better choice. If it ends up doing what I need it to, I’ll close this PR without merging.

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

Successfully merging this pull request may close these issues.

3 participants