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

Named palettes yield unexpected result #855

Closed
banesullivan opened this issue May 11, 2022 · 9 comments
Closed

Named palettes yield unexpected result #855

banesullivan opened this issue May 11, 2022 · 9 comments

Comments

@banesullivan
Copy link
Contributor

I would assume the following two styles to be equivalent

A B
 {
  bands: [
    {band: 1, palette: ['#000', '#f00']},  // red
    {band: 2, palette: ['#000', '#0f0']},  // green
    {band: 3, palette: ['#000', '#00f']}   // blue
  ]
}
{
 bands: [
    {band: 1, palette: 'red'},
    {band: 2, palette: 'green'},
    {band: 3, palette: 'blue'}
  ]
}

However, they are not and produce totally different thumbnails:

A B
thumbnail ee1bc20c-6d63-4751-be0f-8e5fa529ce66

Is this expected? Should "red" be interpreted as equivalent to ['#000', '#f00']

@banesullivan
Copy link
Contributor Author

banesullivan commented May 11, 2022

Notably, C (below) yields the same result as A:

{
 bands: [
    {band: 1, palette: '#f00'},
    {band: 2, palette: '#0f0'},
    {band: 3, palette: '#00f'}
  ]
}
C
thumbnail

@manthey
Copy link
Member

manthey commented May 11, 2022

Does

{
 bands: [
    {band: 1, palette: 'red'},
    {band: 2, palette: '#0f0'},
    {band: 3, palette: 'blue'}
  ]
}

yield the same? green is shorthand for #008000, not #0f0 or #00ff00. Blame css standards for that one...

@banesullivan
Copy link
Contributor Author

🤦🏻‍♂️

Would it make sense to override this in large image having the following in getPaletteColors()?:

    if value == 'green':
        # see https://github.com/girder/large_image/issues/855#issuecomment-
        value = '#0f0'

@manthey
Copy link
Member

manthey commented May 11, 2022

I don't think there is a good answer here -- if we muck with what green means compared to css it will cause surprise. If we don't, it will cause surprise. Maybe we should define R, G, B and RED, GREEN, BLUE to be #f00, #0f0, #00f, since the capitalized versions aren't used.

@banesullivan
Copy link
Contributor Author

if we muck with what green means compared to css it will cause surprise.

Since this is an image-focused library, I think "green" has a specific meaning in this context to refer to the Green channel of an RGB image which should be mapped to #00ff00 and not #008000

IMO, it is more surprising to follow a CSS convention than the convention of the RGB color model here

Maybe we should define R, G, B and RED, GREEN, BLUE to be #f00, #0f0, #00f`, since the capitalized versions aren't used.

That's a fair compromise, but I think the following would be clunky:

{
 bands: [
    {band: 1, palette: 'red'},
    {band: 2, palette: 'GREEN'},
    {band: 3, palette: 'blue'}
  ]
}

Though the following looks nice to me and we could document that R, G, and B should be used when wanting to map like the channels of an RGB image

{
 bands: [
    {band: 1, palette: 'R'},
    {band: 2, palette: 'G'},
    {band: 3, palette: 'B'}
  ]
}

@banesullivan
Copy link
Contributor Author

No matter what, this should be documented in docs/tilesource_options.rst

@manthey
Copy link
Member

manthey commented May 11, 2022

Since this is an image-focused library, I think "green" has a specific meaning in this context to refer to the Green channel of an RGB image which should be mapped to #00ff00 and not #008000

IMO, it is more surprising to follow a CSS convention than the convention of the RGB color model here

Except that a lot of the names are from css (e.g., purple is half-intensity magenta, violet seems to have little to do with the spectral color).

Maybe we should define R, G, B and RED, GREEN, BLUE to be #f00, #0f0, #00f, since the capitalized versions aren't used.

That's a fair compromise, but I think the following would be clunky:

Yes -- that's why using all caps would be mapped for red and blue, too. r, g, b are synonyms to red, green, blue, so we can't reuse those.

@jeffbaumes
Copy link
Member

IMO the palette spec could be designed better. The priority order and all the things a string value could represent seems potentially confusing to users.

def getPaletteColors(value):
    """
    Given a list or a name, return a list of colors in the form of a numpy
    array of RGBA.  If a list, each entry is a color name resolvable by either
    PIL.ImageColor.getcolor, by matplotlib.colors, or a 3 or 4 element list or
    tuple of RGB(A) values on a scale of 0-1.  If this is NOT a list, then, if
    it can be parsed as a color, it is treated as ['#000', <value>].  If that
    cannot be parsed, then it is assumed to be a named palette in palettable
    (such as viridis.Viridis_12) or a named palette in matplotlib (including
    plugins).
    :param value: Either a list, a single color name, or a palette name.  See
        above.
    :returns: a numpy array of RGBA value on the scale of [0-255].
    """

As one idea, I'd suggest new specification modes for the different conditionals so the function does not need to interpret opaque strings in so many ways, something like this:

{
 bands: [
    {band: 1, palette: {channel: 'green'}}, // #000 to #0f0
    {band: 2, palette: {matplotlib: 'jet'}},
    {band: 3, palette: {palettable: 'Earth_7'}},
    {band: 4, palette: ['red', '#112233', 'purple', 'green']}, // interpreted as CSS colors
  ]
}

Single-string palettes like palette: 'green' or palette: '#0f0' would not be allowed (or we allow them for now with existing behavior but deprecate it in the future).

This would still let someone do ['#000', 'green'] and get something perhaps unexpected, but I can't see a way around that because you probably want to support CSS colors in palette arrays.

@manthey
Copy link
Member

manthey commented Aug 5, 2022

This was resolved in #858.

@manthey manthey closed this as completed Aug 5, 2022
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

No branches or pull requests

3 participants