From 409bdd8e3fdcff74fbaad0946a5912098aede4ef Mon Sep 17 00:00:00 2001 From: David Manthey Date: Thu, 9 Mar 2023 19:55:37 -0500 Subject: [PATCH] Speed up validating color values. --- girder_annotation/docs/annotations.rst | 9 ++++----- .../models/annotation.py | 11 +++++++---- .../web_client/views/imageViewerWidget/geojs.js | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/girder_annotation/docs/annotations.rst b/girder_annotation/docs/annotations.rst index d40448486..3376af761 100644 --- a/girder_annotation/docs/annotations.rst +++ b/girder_annotation/docs/annotations.rst @@ -387,11 +387,10 @@ Component Values Colors ~~~~~~ -Colors are specified using a css-like string. Specifically, values of -the form ``#RRGGBB`` and ``#RGB`` are allowed where ``R``, ``G``, and -``B`` are case-insensitive hexadecimal digits. Additionally, values of -the form ``rgb(123, 123, 123)`` and ``rgba(123, 123, 123, 0.123)`` are -allowed, where the colors are specified on a [0-255] integer scale, and +Colors are specified using a css-like string. Specifically, values of the form ``#RRGGBB``, ``#RGB``, ``#RRGGBBAA``, and ``#RGBA`` are allowed where ``R``, +``G``, ``B``, and ``A`` are case-insensitive hexadecimal digits. Additionally, +values of the form ``rgb(123, 123, 123)`` and ``rgba(123, 123, 123, 0.123)`` +are allowed, where the colors are specified on a [0-255] integer scale, and the opacity is specified as a [0-1] floating-point number. Coordinates diff --git a/girder_annotation/girder_large_image_annotation/models/annotation.py b/girder_annotation/girder_large_image_annotation/models/annotation.py index b3e286a3e..07aa7725e 100644 --- a/girder_annotation/girder_large_image_annotation/models/annotation.py +++ b/girder_annotation/girder_large_image_annotation/models/annotation.py @@ -85,14 +85,17 @@ class AnnotationSchema: colorSchema = { 'type': 'string', # We accept colors of the form - # #aabbcc six digit RRGGBB hex - # #abc three digit RGB hex + # #rrggbb six digit RRGGBB hex + # #rgb three digit RGB hex + # #rrggbbaa eight digit RRGGBBAA hex + # #rgba four digit RGBA hex # rgb(255, 255, 255) rgb decimal triplet # rgba(255, 255, 255, 1) rgba quad with RGB in the range [0-255] and # alpha [0-1] # TODO: make rgb and rgba spec validate that rgb is [0-255] and a is # [0-1], rather than just checking if they are digits and such. - 'pattern': r'^(#[0-9a-fA-F]{3,6}|rgb\(\d+,\s*\d+,\s*\d+\)|' + 'pattern': r'^(#([0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})|' + r'rgb\(\d+,\s*\d+,\s*\d+\)|' r'rgba\(\d+,\s*\d+,\s*\d+,\s*(\d?\.|)\d+\))$', } @@ -1009,7 +1012,7 @@ def _similarElementStructure(self, a, b, parentKey=None): # noqa if k == 'id': if not isinstance(b[k], str) or not self.idRegex.match(b[k]): return False - elif parentKey == 'user': + elif parentKey in {'user'} or k in {'fillColor', 'lineColor'}: continue elif parentKey != 'label' or k != 'value': if not self._similarElementStructure(a[k], b[k], k): diff --git a/girder_annotation/girder_large_image_annotation/web_client/views/imageViewerWidget/geojs.js b/girder_annotation/girder_large_image_annotation/web_client/views/imageViewerWidget/geojs.js index bc84b551c..80317a5df 100644 --- a/girder_annotation/girder_large_image_annotation/web_client/views/imageViewerWidget/geojs.js +++ b/girder_annotation/girder_large_image_annotation/web_client/views/imageViewerWidget/geojs.js @@ -165,7 +165,7 @@ var GeojsImageViewerWidgetExtension = function (viewer) { _addPixelmapLayerParams(layerParams, pixelmapElement, levelDifference) { // For pixelmap overlays, there are additional parameters to set layerParams.keepLower = false; - if (typeof layerParams.url === 'function' || levelDifference) { + if (_.isFunction(layerParams.url) || levelDifference) { layerParams.url = (x, y, z) => 'api/v1/item/' + pixelmapElement.girderId + `/tiles/zxy/${z - levelDifference}/${x}/${y}?encoding=PNG`; } else { layerParams.url = layerParams.url + '?encoding=PNG';