Repository: napari/napari

Issue:
Napari viewer does not start 
## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

```
/home/czaki/.pyenv/versions/napari/bin/python /home/czaki/Projekty/napari/examples/add_image.py
Traceback (most recent call last):
  File "/home/czaki/Projekty/napari/examples/add_image.py", line 10, in <module>
    viewer = napari.view_image(data.astronaut(), rgb=True)
  File "/home/czaki/Projekty/napari/napari/view_layers.py", line 134, in view_image
    return _make_viewer_then('add_image', args, kwargs)
  File "/home/czaki/Projekty/napari/napari/view_layers.py", line 115, in _make_viewer_then
    viewer = Viewer(**vkwargs)
  File "/home/czaki/Projekty/napari/napari/viewer.py", line 52, in __init__
    self._window = Window(self, show=show)
  File "/home/czaki/Projekty/napari/napari/_qt/qt_main_window.py", line 420, in __init__
    self._qt_window = _QtMainWindow(viewer)
  File "/home/czaki/Projekty/napari/napari/_qt/qt_main_window.py", line 76, in __init__
    self.qt_viewer = QtViewer(viewer, show_welcome_screen=True)
  File "/home/czaki/Projekty/napari/napari/_qt/qt_viewer.py", line 186, in __init__
    self._create_canvas()
  File "/home/czaki/Projekty/napari/napari/_qt/qt_viewer.py", line 286, in _create_canvas
    self.canvas.bgcolor = get_theme(
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/util/frozen.py", line 17, in __setattr__
    object.__setattr__(self, key, value)
  File "/home/czaki/Projekty/napari/napari/_vispy/vispy_canvas.py", line 78, in bgcolor
    SceneCanvas.bgcolor.fset(self, _value)
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/scene/canvas.py", line 180, in bgcolor
    self._bgcolor = Color(color).rgba
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/color/color_array.py", line 389, in __init__
    rgba = _user_to_rgba(color, clip=clip)
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/color/color_array.py", line 64, in _user_to_rgba
    raise ValueError("Color values must be between 0 and 1 (or use "
ValueError: Color values must be between 0 and 1 (or use clip=True to automatically clip the values).

Process finished with exit code 1
```

## To Reproduce

Steps to reproduce the behavior:

1. change theme to light
2. checkout 044beba342ef392f4cbed2e8e3a27f27d4799ccb
3. start napari viewer

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

Viewer should open, not fail on data set. 

<!-- A clear and concise description of what you expected to happen. -->

## Environment

 - Please copy and paste the information at napari info option in help menubar here:

napari: 0.4.11rc3.dev10+g044beba3.d20210830
Platform: Linux-5.11.0-31-generic-x86_64-with-glibc2.2.5
System: Ubuntu 21.04
Python: 3.8.3 (default, Dec  6 2020, 00:47:20)  [GCC 10.2.0]
Qt: 5.15.2
PyQt5: 5.15.4
NumPy: 1.16.5
SciPy: 1.5.1
Dask: 2021.05.0
VisPy: 0.8.0

OpenGL:
  - GL version:  4.6.0 NVIDIA 470.57.02
  - MAX_TEXTURE_SIZE: 32768

Screens:
  - screen 1: resolution 2560x1440, scale 1.0
  - screen 2: resolution 1920x1080, scale 1.0
  - screen 3: resolution 1920x1080, scale 1.0

Plugins:
  - animation: 0.0.2
  - console: 0.0.3
  - scikit-image: 0.4.11rc3.dev10+g044beba3.d20210830
  - svg: 0.1.5  

 - Any other relevant information:



## Additional context

Fast check shows that bug was introduced in #3034

<!-- Add any other context about the problem here. -->

Napari viewer does not start 
## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

```
/home/czaki/.pyenv/versions/napari/bin/python /home/czaki/Projekty/napari/examples/add_image.py
Traceback (most recent call last):
  File "/home/czaki/Projekty/napari/examples/add_image.py", line 10, in <module>
    viewer = napari.view_image(data.astronaut(), rgb=True)
  File "/home/czaki/Projekty/napari/napari/view_layers.py", line 134, in view_image
    return _make_viewer_then('add_image', args, kwargs)
  File "/home/czaki/Projekty/napari/napari/view_layers.py", line 115, in _make_viewer_then
    viewer = Viewer(**vkwargs)
  File "/home/czaki/Projekty/napari/napari/viewer.py", line 52, in __init__
    self._window = Window(self, show=show)
  File "/home/czaki/Projekty/napari/napari/_qt/qt_main_window.py", line 420, in __init__
    self._qt_window = _QtMainWindow(viewer)
  File "/home/czaki/Projekty/napari/napari/_qt/qt_main_window.py", line 76, in __init__
    self.qt_viewer = QtViewer(viewer, show_welcome_screen=True)
  File "/home/czaki/Projekty/napari/napari/_qt/qt_viewer.py", line 186, in __init__
    self._create_canvas()
  File "/home/czaki/Projekty/napari/napari/_qt/qt_viewer.py", line 286, in _create_canvas
    self.canvas.bgcolor = get_theme(
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/util/frozen.py", line 17, in __setattr__
    object.__setattr__(self, key, value)
  File "/home/czaki/Projekty/napari/napari/_vispy/vispy_canvas.py", line 78, in bgcolor
    SceneCanvas.bgcolor.fset(self, _value)
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/scene/canvas.py", line 180, in bgcolor
    self._bgcolor = Color(color).rgba
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/color/color_array.py", line 389, in __init__
    rgba = _user_to_rgba(color, clip=clip)
  File "/home/czaki/.pyenv/versions/napari/lib/python3.8/site-packages/vispy/color/color_array.py", line 64, in _user_to_rgba
    raise ValueError("Color values must be between 0 and 1 (or use "
ValueError: Color values must be between 0 and 1 (or use clip=True to automatically clip the values).

Process finished with exit code 1
```

## To Reproduce

Steps to reproduce the behavior:

1. change theme to light
2. checkout 044beba342ef392f4cbed2e8e3a27f27d4799ccb
3. start napari viewer

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

Viewer should open, not fail on data set. 

<!-- A clear and concise description of what you expected to happen. -->

## Environment

 - Please copy and paste the information at napari info option in help menubar here:

napari: 0.4.11rc3.dev10+g044beba3.d20210830
Platform: Linux-5.11.0-31-generic-x86_64-with-glibc2.2.5
System: Ubuntu 21.04
Python: 3.8.3 (default, Dec  6 2020, 00:47:20)  [GCC 10.2.0]
Qt: 5.15.2
PyQt5: 5.15.4
NumPy: 1.16.5
SciPy: 1.5.1
Dask: 2021.05.0
VisPy: 0.8.0

OpenGL:
  - GL version:  4.6.0 NVIDIA 470.57.02
  - MAX_TEXTURE_SIZE: 32768

Screens:
  - screen 1: resolution 2560x1440, scale 1.0
  - screen 2: resolution 1920x1080, scale 1.0
  - screen 3: resolution 1920x1080, scale 1.0

Plugins:
  - animation: 0.0.2
  - console: 0.0.3
  - scikit-image: 0.4.11rc3.dev10+g044beba3.d20210830
  - svg: 0.1.5  

 - Any other relevant information:



## Additional context

Fast check shows that bug was introduced in #3034

<!-- Add any other context about the problem here. -->



Discussion:
Thanks for the report @Czaki 
We'll check this out now, @lukasz-migas might have ideas on a fix too
Thanks for the report @Czaki 
We'll check this out now, @lukasz-migas might have ideas on a fix too

Resolving patch:
diff --git a/napari/_qt/qt_viewer.py b/napari/_qt/qt_viewer.py
--- a/napari/_qt/qt_viewer.py
+++ b/napari/_qt/qt_viewer.py
@@ -13,6 +13,7 @@
 from ..components.layerlist import LayerList
 from ..utils import config, perf
 from ..utils.action_manager import action_manager
+from ..utils.colormaps.standardize_color import transform_color
 from ..utils.history import (
     get_open_history,
     get_save_history,
@@ -283,9 +284,9 @@ def _create_canvas(self) -> None:
         self.canvas.connect(self.on_mouse_wheel)
         self.canvas.connect(self.on_draw)
         self.canvas.connect(self.on_resize)
-        self.canvas.bgcolor = get_theme(
-            self.viewer.theme, False
-        ).canvas.as_rgb_tuple()
+        self.canvas.bgcolor = transform_color(
+            get_theme(self.viewer.theme, False).canvas.as_hex()
+        )[0]
         theme = self.viewer.events.theme
 
         on_theme_change = self.canvas._on_theme_change


Test patch:
diff --git a/napari/_qt/_tests/test_qt_viewer.py b/napari/_qt/_tests/test_qt_viewer.py
--- a/napari/_qt/_tests/test_qt_viewer.py
+++ b/napari/_qt/_tests/test_qt_viewer.py
@@ -16,8 +16,10 @@
     layer_test_data,
     skip_local_popups,
 )
+from napari.settings import get_settings
 from napari.utils.interactions import mouse_press_callbacks
 from napari.utils.io import imread
+from napari.utils.theme import available_themes
 
 
 def test_qt_viewer(make_napari_viewer):
@@ -484,3 +486,16 @@ def test_leaks_labels(qtbot, make_napari_viewer):
     assert not gc.collect()
     assert not lr()
     assert not dr()
+
+
+@pytest.mark.parametrize("theme", available_themes())
+def test_canvas_color(make_napari_viewer, theme):
+    """Test instantiating viewer with different themes.
+
+    See: https://github.com/napari/napari/issues/3278
+    """
+    # This test is to make sure the application starts with
+    # with different themes
+    get_settings().appearance.theme = theme
+    viewer = make_napari_viewer()
+    assert viewer.theme == theme