def test_annotation(): geom = box(10, 20, 30, 40) annot = ParsedAnnotation(geom) assert annot.is_fill_grayscale is True assert annot.is_stroke_grayscale is True assert annot.bounds == (10, 20, 30, 40) grey = Color("grey") red = Color("red") annot = ParsedAnnotation(geom, fill_color=red, stroke_color=grey) assert annot.is_fill_grayscale is False assert annot.is_stroke_grayscale is True assert annot.is_grayscale is False annot = ParsedAnnotation(geom, fill_color=grey, stroke_color=grey) assert annot.is_grayscale is True
def parse_colormap_id(colormap_id: ColormapId, existing_colormaps: ColormapsByName, default_color: Optional[Color]) -> Optional[Colormap]: """ Parse a colormap ID to a valid colormap (or None). If the parsed ID is a valid colormap which is not registered in the existing colormaps, the valid colormap is added to the set of existing ones as a side effect. Parameters ---------- colormap_id existing_colormaps Existing colormaps default_color The color for a monotonic linear colormap if the colormap ID is `ColormapEnum.DEFAULT`. Returns ------- colormap The parsed colormap. If None, no colormap has to be applied. Raises ------ ColormapNotFoundProblem If the colormap ID cannot be associated to any colormap. """ if colormap_id == ColormapEnum.NONE: return None elif colormap_id == ColormapEnum.DEFAULT: if default_color is None: return None colormap_id = str(default_color).upper() elif colormap_id == ColormapEnum.DEFAULT_INVERTED: if default_color is None: return existing_colormaps.get('!WHITE') colormap_id = '!' + str(default_color).upper() else: colormap_id = colormap_id.upper() # noqa colormap = existing_colormaps.get(str(colormap_id)) if colormap is None: inverted = colormap_id[0] == "!" color = colormap_id[1:] if inverted else colormap_id try: parsed_color = Color(color) except ValueError: raise ColormapNotFoundProblem(colormap_id) colormap = ColorColormap(parsed_color, inverted=inverted) existing_colormaps[colormap.identifier] = colormap return colormap
def test_parse_annotation(): annot = {"geometry": "POINT (10 10)"} assert parse_annotation(**annot) == ParsedAnnotation(Point(10, 10)) red = Color("red") white = Color("white") annot = {"geometry": "POINT (10 10)", "fill_color": white} default = {"fill_color": red, "stroke_color": white} assert parse_annotation(**annot, default=default) == ParsedAnnotation( Point(10, 10), white, white) assert parse_annotation(**annot) == ParsedAnnotation(Point(10, 10), white) assert parse_annotation(**annot, ignore_fields=['fill_color']) == ParsedAnnotation( Point(10, 10)) assert parse_annotation(**annot, ignore_fields=['fill_color'], default=default) == ParsedAnnotation( Point(10, 10), stroke_color=white)
def test_annotation_list(): grey = Color("grey") white = Color("white") red = Color("red") annot1 = ParsedAnnotation(box(10, 20, 30, 40), fill_color=white, stroke_color=red) annot2 = ParsedAnnotation(box(5, 100, 20, 200), fill_color=grey, stroke_color=white) al = ParsedAnnotations() al.append(annot1) al.append(annot2) assert len(al) == 2 assert al[1] == annot2 assert al.is_fill_grayscale is True assert al.is_stroke_grayscale is False assert al.is_grayscale is False assert al.bounds == (5, 20, 30, 200)
class Annotation(BaseModel): geometry: str = Field( ..., description='A geometry described in Well-known text (WKT)', example='POINT(10 10)', ) fill_color: Optional[Color] = Field( Color("white"), description='A color to fill the annotation', example='#FF00FF') stroke_color: Optional[Color] = Field( None, description='A color for the annotation stroke') stroke_width: Optional[conint(ge=0, le=10)] = Field( 0, description='A width for the annotation stroke')
def test_parse_colormap_id(): red = Color("red") assert parse_colormap_id(ColormapEnum.NONE, ALL_COLORMAPS, red) is None assert parse_colormap_id( ColormapEnum.DEFAULT, ALL_COLORMAPS, red ).identifier == 'RED' assert parse_colormap_id( ColormapEnum.DEFAULT_INVERTED, ALL_COLORMAPS, red ).identifier == '!RED' assert parse_colormap_id('JET', ALL_COLORMAPS, red).identifier == 'JET' assert parse_colormap_id('!JET', ALL_COLORMAPS, red).identifier == '!JET' assert parse_colormap_id('blue', ALL_COLORMAPS, red).identifier == 'BLUE' assert parse_colormap_id('!blue', ALL_COLORMAPS, red).identifier == '!BLUE' assert parse_colormap_id('!0x0f0', ALL_COLORMAPS, red).identifier == '!LIME' assert '#ABCDEF' not in ALL_COLORMAPS assert parse_colormap_id('#abcdef', ALL_COLORMAPS, red).identifier == '#ABCDEF' assert '#ABCDEF' in ALL_COLORMAPS with pytest.raises(ColormapNotFoundProblem): parse_colormap_id('brol', ALL_COLORMAPS, red)
# Non-trivial colormaps COLORMAPS = {} for ctype, cmaps in mpl_cmaps.items(): for cmap in cmaps: for inv in (False, True): colormap = MatplotlibColormap(cmap, cmap_type=ctype, inverted=inv) COLORMAPS[colormap.identifier] = colormap # Pre-load colormaps for named colors COLOR_COLORMAPS = {} for name in COLORS_BY_NAME: for inv in (False, True): colormap = ColorColormap(Color(name), inverted=inv) COLOR_COLORMAPS[colormap.identifier] = colormap # All pre-loaded colormaps ALL_COLORMAPS = {**COLORMAPS, **COLOR_COLORMAPS} # Default colormaps per channel index DEFAULT_CHANNEL_COLORMAPS = { 0: ALL_COLORMAPS['RED'], 1: ALL_COLORMAPS['LIME'], 2: ALL_COLORMAPS['BLUE'], 3: ALL_COLORMAPS['CYAN'], 4: ALL_COLORMAPS['MAGENTA'], 5: ALL_COLORMAPS['YELLOW'] }
def test_rgba_int_conversion(): assert np.allclose(Color(Color( (10, 255, 0, 0.5)).as_int()).as_rgb_tuple(alpha=True), (10, 255, 0, 0.5), atol=1e-2)
def test_rgb_int_conversion(): assert np.array_equal( Color(Color((10, 255, 0)).as_int()).as_rgb_tuple(alpha=False), (10, 255, 0))