From c9f041e4bbc39d9f4e88463d5220b9cfc24fa895 Mon Sep 17 00:00:00 2001 From: Tim Rheinfels <tim.rheinfels@fau.de> Date: Wed, 24 May 2023 11:32:02 +0200 Subject: [PATCH] visualization: add color utilities --- src/util.py | 3 +++ src/visualization/color.py | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/visualization/color.py diff --git a/src/util.py b/src/util.py index 73ec909..6597ddc 100644 --- a/src/util.py +++ b/src/util.py @@ -20,10 +20,13 @@ ### @author Tim Rheinfels <tim.rheinfels@fau.de> ### +import colorsys import cvxpy as cp from enum import Enum import logging import json +import matplotlib as mpl +import matplotlib.pyplot as plot import numpy as np ### diff --git a/src/visualization/color.py b/src/visualization/color.py new file mode 100644 index 0000000..c364b38 --- /dev/null +++ b/src/visualization/color.py @@ -0,0 +1,52 @@ +## This file is part of the simulative evaluation for the qronos observer abstractions. +## Copyright (C) 2022-2023 Tim Rheinfels <tim.rheinfels@fau.de> +## See https://gitlab.cs.fau.de/qronos-state-abstractions/simulation +## +## Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +## +## 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +## +## 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +## +## 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +### +### @file visualization/color.py +### +### @brief Provides color utilities for visualization +### +### @author Tim Rheinfels <tim.rheinfels@fau.de> +### + +import colorsys +import matplotlib as mpl + +### +### @brief Alters the @p alpha channel of a @p color +### +### @param color Color to modify +### @param alpha Alpha channel in [0, 1] to set +### +### @returns The @p color with new @p alpha value +### +def set_alpha(color, alpha): + return (*mpl.colors.to_rgb(color), alpha) + +### +### @brief Scales the @p lightness channel of a @p color to +### +### @param color Color to modify +### @param lightness Lightness modifier in [0, 1] +### +### @returns The @p color with its @p lightness scaled +### +# +# Adapted from https://stackoverflow.com/questions/37765197/darken-or-lighten-a-color-in-matplotlib +# +def scale_lightness(color, lightness): + # convert rgb to hls + h, l, s = colorsys.rgb_to_hls(*mpl.colors.to_rgb(color)) + # manipulate h, l, s values and return as rgb + return colorsys.hls_to_rgb(h, min(1, l * lightness), s = s) -- GitLab