|
| 1 | +""" |
| 2 | +Color functions |
| 3 | +
|
| 4 | +""" |
| 5 | + |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import palettable |
| 9 | + |
| 10 | +from mathics.core.atoms import Integer0, Integer1, MachineReal, String |
| 11 | +from mathics.core.convert.expression import to_expression |
| 12 | +from mathics.core.element import BaseElement |
| 13 | +from mathics.core.evaluation import Evaluation |
| 14 | +from mathics.core.expression import Expression |
| 15 | +from mathics.core.list import ListExpression |
| 16 | +from mathics.core.symbols import Symbol |
| 17 | +from mathics.core.systemsymbols import ( |
| 18 | + SymbolBlend, |
| 19 | + SymbolColorData, |
| 20 | + SymbolFunction, |
| 21 | + SymbolMap, |
| 22 | + SymbolRGBColor, |
| 23 | + SymbolSlot, |
| 24 | +) |
| 25 | + |
| 26 | +SymbolColorDataFunction = Symbol("ColorDataFunction") |
| 27 | + |
| 28 | + |
| 29 | +class _GradientColorScheme: |
| 30 | + def colors(self) -> list: |
| 31 | + """return the list of colors""" |
| 32 | + raise NotImplementedError |
| 33 | + |
| 34 | + def color_data_function(self, name: str) -> Expression: |
| 35 | + """Return a function that compute the colors""" |
| 36 | + colors = ListExpression( |
| 37 | + *[ |
| 38 | + to_expression( |
| 39 | + SymbolRGBColor, *color, elements_conversion_fn=MachineReal |
| 40 | + ) |
| 41 | + for color in self.colors() |
| 42 | + ] |
| 43 | + ) |
| 44 | + blend = Expression( |
| 45 | + SymbolFunction, |
| 46 | + Expression(SymbolBlend, colors, Expression(SymbolSlot, Integer1)), |
| 47 | + ) |
| 48 | + arguments = [ |
| 49 | + String(name), |
| 50 | + String("Gradients"), |
| 51 | + ListExpression(Integer0, Integer1), |
| 52 | + blend, |
| 53 | + ] |
| 54 | + return Expression(SymbolColorDataFunction, *arguments) |
| 55 | + |
| 56 | + |
| 57 | +class _PalettableGradient(_GradientColorScheme): |
| 58 | + def __init__(self, palette, is_reversed: bool): |
| 59 | + self.palette = palette |
| 60 | + self.reversed = is_reversed |
| 61 | + |
| 62 | + def colors(self) -> list: |
| 63 | + colors = self.palette.mpl_colors |
| 64 | + if self.reversed: |
| 65 | + colors = list(reversed(colors)) |
| 66 | + return colors |
| 67 | + |
| 68 | + |
| 69 | +class _PredefinedGradient(_GradientColorScheme): |
| 70 | + _colors: list |
| 71 | + |
| 72 | + def __init__(self, colors: list): |
| 73 | + self._colors = colors |
| 74 | + |
| 75 | + def colors(self) -> list: |
| 76 | + return self._colors |
| 77 | + |
| 78 | + |
| 79 | +COLOR_PALETTES = { |
| 80 | + "LakeColors": _PredefinedGradient( |
| 81 | + [ |
| 82 | + (0.293416, 0.0574044, 0.529412), |
| 83 | + (0.563821, 0.527565, 0.909499), |
| 84 | + (0.762631, 0.846998, 0.914031), |
| 85 | + (0.941176, 0.906538, 0.834043), |
| 86 | + ] |
| 87 | + ), |
| 88 | + "Pastel": _PalettableGradient(palettable.colorbrewer.qualitative.Pastel1_9, False), |
| 89 | + "Rainbow": _PalettableGradient(palettable.colorbrewer.diverging.Spectral_9, True), |
| 90 | + "RedBlueTones": _PalettableGradient( |
| 91 | + palettable.colorbrewer.diverging.RdBu_11, False |
| 92 | + ), |
| 93 | + "GreenPinkTones": _PalettableGradient( |
| 94 | + palettable.colorbrewer.diverging.PiYG_9, False |
| 95 | + ), |
| 96 | + "GrayTones": _PalettableGradient(palettable.colorbrewer.sequential.Greys_9, False), |
| 97 | + "SolarColors": _PalettableGradient(palettable.colorbrewer.sequential.OrRd_9, True), |
| 98 | + "CherryTones": _PalettableGradient(palettable.colorbrewer.sequential.Reds_9, True), |
| 99 | + "FuchsiaTones": _PalettableGradient(palettable.colorbrewer.sequential.RdPu_9, True), |
| 100 | + "SiennaTones": _PalettableGradient( |
| 101 | + palettable.colorbrewer.sequential.Oranges_9, True |
| 102 | + ), |
| 103 | + # specific to Mathics |
| 104 | + "Paired": _PalettableGradient(palettable.colorbrewer.qualitative.Paired_9, False), |
| 105 | + "Accent": _PalettableGradient(palettable.colorbrewer.qualitative.Accent_8, False), |
| 106 | + "Aquatic": _PalettableGradient(palettable.wesanderson.Aquatic1_5, False), |
| 107 | + "Zissou": _PalettableGradient(palettable.wesanderson.Zissou_5, False), |
| 108 | + "Tableau": _PalettableGradient(palettable.tableau.Tableau_20, False), |
| 109 | + "TrafficLight": _PalettableGradient(palettable.tableau.TrafficLight_9, False), |
| 110 | + "Moonrise1": _PalettableGradient(palettable.wesanderson.Moonrise1_5, False), |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +def get_color_palette(name, evaluation): |
| 115 | + palette = COLOR_PALETTES.get(name, None) |
| 116 | + if palette is None: |
| 117 | + evaluation.message("ColorData", "notent", name) |
| 118 | + return None |
| 119 | + return palette.colors() |
| 120 | + |
| 121 | + |
| 122 | +def gradient_palette( |
| 123 | + color_function: BaseElement, n: int, evaluation: Evaluation |
| 124 | +) -> Optional[list]: # always returns RGB values |
| 125 | + """Return a list of rgb color components""" |
| 126 | + if isinstance(color_function, String): |
| 127 | + color_data = Expression(SymbolColorData, color_function).evaluate(evaluation) |
| 128 | + if not color_data.has_form("ColorDataFunction", 4): |
| 129 | + return None |
| 130 | + _, kind, interval, blend = color_data.elements |
| 131 | + if not isinstance(kind, String) or kind.get_string_value() != "Gradients": |
| 132 | + return None |
| 133 | + if not interval.has_form("List", 2): |
| 134 | + return None |
| 135 | + x0, x1 = (x.round_to_float() for x in interval.elements) |
| 136 | + else: |
| 137 | + blend = color_function |
| 138 | + x0 = 0.0 |
| 139 | + x1 = 1.0 |
| 140 | + |
| 141 | + xd = x1 - x0 |
| 142 | + offsets = [MachineReal(x0 + float(xd * i) / float(n - 1)) for i in range(n)] |
| 143 | + colors = Expression(SymbolMap, blend, ListExpression(*offsets)).evaluate(evaluation) |
| 144 | + if len(colors.elements) != n: |
| 145 | + return None |
| 146 | + |
| 147 | + from mathics.builtin.colors.color_directives import ColorError, expression_to_color |
| 148 | + |
| 149 | + try: |
| 150 | + objects = [expression_to_color(x) for x in colors.elements] |
| 151 | + if any(x is None for x in objects): |
| 152 | + return None |
| 153 | + return [x.to_rgba()[:3] for x in objects] |
| 154 | + except ColorError: |
| 155 | + return None |
| 156 | + |
| 157 | + |
| 158 | +# color-blind friendly palette from https://davidmathlogic.com/colorblind |
| 159 | +# palette3 is for 3d plots, i.e. surfaces |
| 160 | +palette3 = [ |
| 161 | + (255, 176, 0), # orange |
| 162 | + (100, 143, 255), # blue |
| 163 | + (220, 38, 127), # red |
| 164 | + (50, 150, 140), # green |
| 165 | + (120, 94, 240), # purple |
| 166 | + (254, 97, 0), # dark orange |
| 167 | + (0, 114, 178), # dark blue |
| 168 | +] |
| 169 | + |
| 170 | + |
| 171 | +# palette 2 is for 2d plots, i.e. lines |
| 172 | +# same colors as palette3 but in a little different order |
| 173 | +palette2 = [ |
| 174 | + (100, 143, 255), # blue |
| 175 | + (255, 176, 0), # orange |
| 176 | + (50, 150, 140), # green |
| 177 | + (220, 38, 127), # red |
| 178 | + (120, 94, 240), # purple |
| 179 | + (254, 97, 0), # dark orange |
| 180 | + (0, 114, 178), # dark blue |
| 181 | +] |
| 182 | + |
| 183 | + |
| 184 | +def palette_color_directive(palette, i, opacity=None): |
| 185 | + """returns a directive in a form suitable for GraphicsGenerator.add_directives""" |
| 186 | + """ for setting the color of an entire entity such as a line or surface """ |
| 187 | + rgb = palette[i % len(palette)] |
| 188 | + rgb = [c / 255.0 for c in rgb] |
| 189 | + if opacity is not None: |
| 190 | + rgb.append(opacity) |
| 191 | + return [SymbolRGBColor, *rgb] |
0 commit comments