Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Jan 27, 2023
2 parents fc128f2 + 75a2d7f commit 38fbf13
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
__application_name__ = "Colour - Dash"

__major_version__ = "0"
__minor_version__ = "1"
__change_version__ = "15"
__minor_version__ = "2"
__change_version__ = "0"
__version__ = ".".join(
(__major_version__, __minor_version__, __change_version__)
)
Expand Down
134 changes: 128 additions & 6 deletions apps/rgb_colourspace_chromatically_adapted_primaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import sys
import urllib.parse
from dash.dcc import Dropdown, Link, Markdown, Slider
from contextlib import suppress
from dash.dcc import Dropdown, Link, Location, Markdown, Slider
from dash.dependencies import Input, Output
from dash.html import A, Code, Div, H3, H5, Li, Pre, Ul
from urllib.parse import parse_qs, urlencode, urlparse

from colour.colorimetry import CCS_ILLUMINANTS
from colour.models import RGB_COLOURSPACES, chromatically_adapted_primaries
Expand All @@ -32,8 +34,11 @@
"APP_PATH",
"APP_DESCRIPTION",
"APP_UID",
"DEFAULT_STATE",
"LAYOUT",
"set_primaries_output",
"update_state_on_url_query_change",
"update_url_query_on_state_change",
]

APP_NAME: str = "RGB Colourspace Chromatically Adapted Primaries"
Expand Down Expand Up @@ -61,8 +66,22 @@
App unique id.
"""

DEFAULT_STATE = {
"colourspace": RGB_COLOURSPACE_OPTIONS[0]["value"],
"illuminant": ILLUMINANTS_OPTIONS[0]["value"],
"chromatic_adaptation_transform": CHROMATIC_ADAPTATION_TRANSFORM_OPTIONS[
0
]["value"],
"formatter": "str",
"decimals": 10,
}
"""
Default App state.
"""

LAYOUT: Div = Div(
[
Location(id=f"url-{APP_UID}", refresh=False),
H3([Link(APP_NAME, href=APP_PATH)], className="text-center"),
Div(
[
Expand All @@ -71,23 +90,23 @@
Dropdown(
id=f"colourspace-{APP_UID}",
options=RGB_COLOURSPACE_OPTIONS,
value=RGB_COLOURSPACE_OPTIONS[0]["value"],
value=DEFAULT_STATE["colourspace"],
clearable=False,
className="app-widget",
),
H5(children="Illuminant"),
Dropdown(
id=f"illuminant-{APP_UID}",
options=ILLUMINANTS_OPTIONS,
value=ILLUMINANTS_OPTIONS[0]["value"],
value=DEFAULT_STATE["illuminant"],
clearable=False,
className="app-widget",
),
H5(children="Chromatic Adaptation Transform"),
Dropdown(
id=f"chromatic-adaptation-transform-{APP_UID}",
options=CHROMATIC_ADAPTATION_TRANSFORM_OPTIONS,
value=CHROMATIC_ADAPTATION_TRANSFORM_OPTIONS[0]["value"],
value=DEFAULT_STATE["chromatic_adaptation_transform"],
clearable=False,
className="app-widget",
),
Expand All @@ -98,7 +117,7 @@
{"label": "str", "value": "str"},
{"label": "repr", "value": "repr"},
],
value="str",
value=DEFAULT_STATE["formatter"],
clearable=False,
className="app-widget",
),
Expand All @@ -108,7 +127,7 @@
min=1,
max=15,
step=1,
value=10,
value=DEFAULT_STATE["decimals"],
marks={i + 1: str(i + 1) for i in range(15)},
className="app-widget",
),
Expand Down Expand Up @@ -225,3 +244,106 @@ def set_primaries_output(
P_f = repr(P)

return P_f


@APP.callback(
[
Output(f"colourspace-{APP_UID}", "value"),
Output(f"illuminant-{APP_UID}", "value"),
Output(f"chromatic-adaptation-transform-{APP_UID}", "value"),
Output(f"formatter-{APP_UID}", "value"),
Output(f"decimals-{APP_UID}", "value"),
],
[
Input("url", "href"),
],
)
def update_state_on_url_query_change(href: str) -> tuple:
"""
Update the App state on URL query change.
Parameters
----------
href
URL.
Returns
-------
:class:`tuple`
App state.
"""

parse_result = urlparse(href)

query = parse_qs(parse_result.query)

def value_from_query(value: str) -> str:
"""Return the given value from the query."""

with suppress(KeyError):
return query[value][0]

return DEFAULT_STATE[value.replace("-", "_")]

state = (
value_from_query("colourspace"),
value_from_query("illuminant"),
value_from_query("chromatic-adaptation-transform"),
value_from_query("formatter"),
int(value_from_query("decimals")),
)

return state


@APP.callback(
Output(f"url-{APP_UID}", "search"),
[
Input(f"colourspace-{APP_UID}", "value"),
Input(f"illuminant-{APP_UID}", "value"),
Input(f"chromatic-adaptation-transform-{APP_UID}", "value"),
Input(f"formatter-{APP_UID}", "value"),
Input(f"decimals-{APP_UID}", "value"),
],
)
def update_url_query_on_state_change(
colourspace: str,
illuminant: str,
chromatic_adaptation_transform: str,
formatter: str,
decimals: int,
) -> str:
"""
Update the URL query on App state change.
Parameters
----------
colourspace
*RGB* colourspace to chromatically adapt the *primaries*.
illuminant
*CIE 1931 2 Degree Standard Observer* illuminant to adapt the
*primaries* to.
chromatic_adaptation_transform
*Chromatic adaptation transform* to use.
formatter
Formatter to use, :func:`str`, :func:`repr` or *Nuke*.
decimals
Decimals to use when formatting the chromatically adapted *primaries*.
Returns
-------
:class:`str`
Url query.
"""

query = urlencode(
{
"colourspace": colourspace,
"illuminant": illuminant,
"chromatic-adaptation-transform": chromatic_adaptation_transform,
"formatter": formatter,
"decimals": decimals,
}
)

return f"?{query}"
Loading

0 comments on commit 38fbf13

Please sign in to comment.