Skip to content

Commit

Permalink
Replace colormap code with inbuilt napari colormap functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GenevieveBuckley committed Jun 14, 2024
1 parent 33a9426 commit cb25850
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 51 deletions.
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
12 changes: 4 additions & 8 deletions napari_tiff/napari_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import numpy
from tifffile import PHOTOMETRIC, TiffFile, xml2dict
from vispy.color import Colormap
from napari.utils.colormaps import ALL_COLORMAPS

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 +71,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"]]
name = ["Cyan", "Magenta", "Yellow", "Black"]
visible = [False, False, False, True]
blending = ["additive", "additive", "additive", "additive"]
Expand Down Expand Up @@ -243,7 +239,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 = [ALL_COLORMAPS["red"], ALL_COLORMAPS["green"], ALL_COLORMAPS["blue"]]
name = ["Red", "Green", "Blue", "Alpha"][:n]
blending = ["additive", "additive", "additive", "translucent"][:n]
else:
Expand Down

0 comments on commit cb25850

Please sign in to comment.