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 4 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
49 changes: 3 additions & 46 deletions napari_tiff/napari_tiff_colormaps.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
import numpy
from vispy.color import Colormap


def alpha_colormap(bitspersample=8, samples=4):
"""Return Alpha colormap."""
n = 2**bitspersample
ramp = numpy.linspace(0.0, 1.0, n).astype("float32")
a = numpy.zeros((n, samples), dtype="float32")
a[:, 3] = ramp[::-1]
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)]
alpha_cmap = numpy.zeros((n, samples), dtype="float32")
alpha_cmap[:, 3] = ramp[::-1]
return alpha_cmap


def int_to_rgba(intrgba: int) -> tuple:
Expand Down
14 changes: 4 additions & 10 deletions napari_tiff/napari_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

import numpy
from tifffile import PHOTOMETRIC, TiffFile, xml2dict
from vispy.color import Colormap

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 +69,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 = ["cyan", "magenta", "yellow", "gray"]
name = ["Cyan", "Magenta", "Yellow", "Black"]
visible = [False, False, False, True]
blending = ["additive", "additive", "additive", "additive"]
Expand Down Expand Up @@ -156,7 +150,7 @@ def get_tiff_metadata(tif: TiffFile) -> dict[str, Any]:
colormap = colormap / 65535.0
else:
colormap = colormap / 255.0
colormap = Colormap(colormap.astype("float32").T)
colormap = colormap.astype("float32").T

if colormap is None and page.photometric == 0:
# MINISBLACK
Expand Down Expand Up @@ -243,7 +237,7 @@ def get_imagej_metadata(tif: TiffFile) -> dict[str, Any]:
rgb = False
n = shape[channel_axis]
visible = [True, True, True]
colormap = rgb_colormaps(samples=4)[:n]
colormap = ["red", "green", "blue", alpha_colormap()]
name = ["Red", "Green", "Blue", "Alpha"][:n]
blending = ["additive", "additive", "additive", "translucent"][:n]
else:
Expand Down
2 changes: 1 addition & 1 deletion napari_tiff/napari_tiff_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from tifffile import TIFF, TiffFile, TiffSequence, xml2dict
from tifffile import TIFF, TiffFile, TiffSequence

from napari_tiff.napari_tiff_metadata import get_metadata

Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ dependencies = [
'imagecodecs',
'numpy',
'tifffile>=2023.9.26',
'vispy',
'zarr',
]

Expand Down
Loading