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

Simplify colormaps #31

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 1 addition & 43 deletions napari_tiff/napari_tiff_colormaps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy
from vispy.color import Colormap
from napari.utils import Colormap


def alpha_colormap(bitspersample=8, samples=4):
Expand All @@ -11,48 +11,6 @@ def alpha_colormap(bitspersample=8, samples=4):
return Colormap(a)


def rgb_colormaps(bitspersample=8, samples=3):
"""Return RGB colormaps."""
n = 2**bitspersample
ramp = numpy.linspace(0.0, 1.0, n).astype("float32")
r = numpy.zeros((n, samples), dtype="float32")
r[:, 0] = ramp
g = numpy.zeros((n, samples), dtype="float32")
g[:, 1] = ramp
b = numpy.zeros((n, samples), dtype="float32")
b[:, 2] = ramp
if samples > 3:
r[:, 3:] = 1.0
g[:, 3:] = 1.0
b[:, 3:] = 1.0
return [Colormap(r), Colormap(g), Colormap(b)]


def cmyk_colormaps(bitspersample=8, samples=3):
"""Return CMYK colormaps."""
n = 2**bitspersample
ramp = numpy.linspace(1.0, 0.0, n).astype("float32")
c = numpy.zeros((n, samples), dtype="float32")
c[:, 1] = ramp
c[:, 2] = ramp
m = numpy.zeros((n, samples), dtype="float32")
m[:, 0] = ramp
m[:, 2] = ramp
y = numpy.zeros((n, samples), dtype="float32")
y[:, 0] = ramp
y[:, 1] = ramp
k = numpy.zeros((n, samples), dtype="float32")
k[:, 0] = ramp
k[:, 1] = ramp
k[:, 2] = ramp
if samples > 3:
c[:, 3:] = 1.0
m[:, 3:] = 1.0
y[:, 3:] = 1.0
k[:, 3:] = 1.0
return [Colormap(c), Colormap(m), Colormap(y), Colormap(k)]


def int_to_rgba(intrgba: int) -> tuple:
signed = intrgba < 0
rgba = [x / 255 for x in intrgba.to_bytes(4, signed=signed, byteorder="big")]
Expand Down
13 changes: 4 additions & 9 deletions napari_tiff/napari_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

import numpy
from tifffile import PHOTOMETRIC, TiffFile, xml2dict
from vispy.color import Colormap
from napari.utils.colormaps import ALL_COLORMAPS, Colormap
Czaki marked this conversation as resolved.
Show resolved Hide resolved

from napari_tiff.napari_tiff_colormaps import (
alpha_colormap,
cmyk_colormaps,
int_to_rgba,
rgb_colormaps,
)
from napari_tiff.napari_tiff_colormaps import alpha_colormap, int_to_rgba


def get_metadata(tif: TiffFile) -> dict[str, Any]:
Expand Down Expand Up @@ -75,7 +70,7 @@ def get_tiff_metadata(tif: TiffFile) -> dict[str, Any]:
# CMYK
channel_axis = axes.find("S")
if channel_axis >= 0 and shape[channel_axis] >= 4:
colormap = cmyk_colormaps()
colormap = [ALL_COLORMAPS["cyan"], ALL_COLORMAPS["magenta"], ALL_COLORMAPS["yellow"], ALL_COLORMAPS["gray"]]
GenevieveBuckley marked this conversation as resolved.
Show resolved Hide resolved
name = ["Cyan", "Magenta", "Yellow", "Black"]
visible = [False, False, False, True]
blending = ["additive", "additive", "additive", "additive"]
Expand Down Expand Up @@ -243,7 +238,7 @@ def get_imagej_metadata(tif: TiffFile) -> dict[str, Any]:
rgb = False
n = shape[channel_axis]
visible = [True, True, True]
GenevieveBuckley marked this conversation as resolved.
Show resolved Hide resolved
colormap = rgb_colormaps(samples=4)[:n]
colormap = [ALL_COLORMAPS["red"], ALL_COLORMAPS["green"], ALL_COLORMAPS["blue"]]
GenevieveBuckley marked this conversation as resolved.
Show resolved Hide resolved
name = ["Red", "Green", "Blue", "Alpha"][:n]
blending = ["additive", "additive", "additive", "translucent"][:n]
GenevieveBuckley marked this conversation as resolved.
Show resolved Hide resolved
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ classifiers = [
requires-python = '>=3.10'
dependencies = [
'imagecodecs',
'napari',
Czaki marked this conversation as resolved.
Show resolved Hide resolved
'numpy',
'tifffile>=2023.9.26',
'vispy',
'zarr',
]

Expand Down
Loading