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

Color can potentially by defined in other than RGB colorspace #1194

Open
1 task done
xhruso00 opened this issue May 9, 2024 · 1 comment
Open
1 task done

Color can potentially by defined in other than RGB colorspace #1194

xhruso00 opened this issue May 9, 2024 · 1 comment

Comments

@xhruso00
Copy link

xhruso00 commented May 9, 2024

Describe the bug
hexString for NSColor extension provides invalid values for CMYK colors or other non RGB colors selected using NSColorPicker.

To Reproduce
Steps to reproduce the behavior:
let color = NSColor(deviceCyan: 0, magenta: 1, yellow: 1, black: 0.1, alpha: 1)
print(color.hexString) returns #000000

Expected behavior
print(color.hexString) should return #B50E20

Screenshots

Desktop (please complete the following information):

  • all versions

Additional context
FIX: convert color to RGB colorspace first. Use generic color space

extension NSColor {
    /// SwifterSwift: Hexadecimal value string (read-only).
    var hexString: String {
        var rgbColor : NSColor = self
        if colorSpace.colorSpaceModel != .rgb {
            rgbColor = usingColorSpace(.genericRGB) ?? NSColor.black
        }
        
        let components: [Int] = {
            let comps = rgbColor.cgColor.components!.map { Int($0 * 255.0) }
            guard comps.count != 4 else { return comps }
            return [comps[0], comps[0], comps[0], comps[1]]
        }()
        return String(format: "#%02X%02X%02X", components[0], components[1], components[2])
    }
}
@guykogus
Copy link
Contributor

guykogus commented May 9, 2024

@xhruso00 thanks for catching this. Why don’t you try and submit a PR to fix it?

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

No branches or pull requests

2 participants