# Path: ncdjango/interfaces/arcgis_extended/utils.py
# def get_renderer_from_definition(config):
#     """Returns a renderer object based on the configuration (as a dictionary)"""
# 
#     options = config.get('options', {})
# 
#     try:
#         renderer_type = config['type']
#         renderer_colors = [(float(x[0]), hex_to_color(x[1])) for x in config['colors']]
#         fill_value = options.get('fill_value')
#         if fill_value is not None:
#             fill_value = float(fill_value)
#     except KeyError:
#         raise ValueError("Missing required keys from renderer configuration")
# 
#     renderer_kwargs = {
#         'colormap': renderer_colors,
#         'fill_value': fill_value,
#     }
# 
#     if renderer_type == "stretched":
#         color_space = options.get('color_space', 'hsv').lower().strip()
#         if color_space not in ('rgb', 'hsv'):
#             raise ValueError("Invalid color space: {}".format(color_space))
# 
#         renderer = StretchedRenderer(colorspace=color_space, **renderer_kwargs)
#     elif renderer_type == "classified":
#         renderer = ClassifiedRenderer(**renderer_kwargs)
#     elif renderer_type == "unique":
#         try:
#             labels = [str(x) for x in options.get('labels', [])]
#         except TypeError:
#             raise ValueError("Labels option must be an array")
# 
#         renderer = UniqueValuesRenderer(labels=labels, **renderer_kwargs)
# 
#     return renderer


import json
from django import forms
from django.core.exceptions import ValidationError
from trefoil.render.renderers import RasterRenderer
from .utils import get_renderer_from_definition




class StyleField(forms.Field):
    """Custom renderer configurations"""

    def to_python(self, value):
        if not value or isinstance(value, RasterRenderer):
            return value

        try:

# Path: ncdjango/interfaces/arcgis_extended/utils.py
# def get_renderer_from_definition(config):
#     """Returns a renderer object based on the configuration (as a dictionary)"""
# 
#     options = config.get('options', {})
# 
#     try:
#         renderer_type = config['type']
#         renderer_colors = [(float(x[0]), hex_to_color(x[1])) for x in config['colors']]
#         fill_value = options.get('fill_value')
#         if fill_value is not None:
#             fill_value = float(fill_value)
#     except KeyError:
#         raise ValueError("Missing required keys from renderer configuration")
# 
#     renderer_kwargs = {
#         'colormap': renderer_colors,
#         'fill_value': fill_value,
#     }
# 
#     if renderer_type == "stretched":
#         color_space = options.get('color_space', 'hsv').lower().strip()
#         if color_space not in ('rgb', 'hsv'):
#             raise ValueError("Invalid color space: {}".format(color_space))
# 
#         renderer = StretchedRenderer(colorspace=color_space, **renderer_kwargs)
#     elif renderer_type == "classified":
#         renderer = ClassifiedRenderer(**renderer_kwargs)
#     elif renderer_type == "unique":
#         try:
#             labels = [str(x) for x in options.get('labels', [])]
#         except TypeError:
#             raise ValueError("Labels option must be an array")
# 
#         renderer = UniqueValuesRenderer(labels=labels, **renderer_kwargs)
# 
#     return renderer


# Path: ncdjango/interfaces/arcgis_extended/form_fields.py
import json
from django import forms
from django.core.exceptions import ValidationError
from trefoil.render.renderers import RasterRenderer
from .utils import get_renderer_from_definition





class StyleField(forms.Field):
    """Custom renderer configurations"""

    def to_python(self, value):
        if not value or isinstance(value, RasterRenderer):
            return value

        try:

            return {int(k): get_renderer_from_definition(v) for k, v in json.loads(value).items()}