<commit_before>from . import Core

from Blit import Color, Bitmap

class Provider:
    """
    """
    def __init__(self, layer, stack):
        self.config = layer.config
        self.stack = stack
    
    def renderTile(self, width, height, srs, coord):
        
        # start with an empty base
        rendered = Color(0, 0, 0, 0x10)
        
        # a place to put rendered tiles
        tiles = dict()
        
        for layer in self.stack:
            #
            # Prepare pixels from elsewhere.
            #
            
            source_name, mask_name, color_name = [layer.get(k, None) for k in ('src', 'mask', 'color')]
        
            if source_name and color_name and mask_name:
                raise Core.KnownUnknown("You can't specify src, color and mask together in a Sandwich Layer: %s, %s, %s" % (repr(source_name), repr(color_name), repr(mask_name)))
            
            if source_name and source_name not in tiles:
                provider = self.config.layers[source_name].provider
                tiles[source_name] = Bitmap(provider.renderTile(width, height, srs, coord))
            
            if mask_name and mask_name not in tiles:
                provider = self.config.layers[mask_name].provider
                tiles[mask_name] = Bitmap(provider.renderTile(width, height, srs, coord))
            
            #
            # Build up the foreground layer.
            #
            
            if source_name and color_name:
                # color first, then layer
                foreground = make_color(color_name).blend(tiles[source_name])
            
            elif source_name:
                foreground = tiles[source_name]
            
            elif color_name:
                foreground = make_color(color_name)
    
            elif mask_name:
                raise Core.KnownUnknown("You have to provide more than just a mask to Sandwich Layer: %s" % repr(mask_name))
    
            else:
                raise Core.KnownUnknown("You have to provide at least some combination of src, color and mask to Sandwich Layer")
            
            #
            # Do the final composition.
            #
            
            kwargs = dict(opacity=float(layer.get('opacity', 1.0)))
            
            if mask_name:
                rendered = rendered.blend(foreground, tiles[mask_name], **kwargs)
            
            else:
                rendered = rendered.blend(foreground, **kwargs)
    
        #
        
        if rendered.size() == (width, height):
            return rendered.image()
        else:
            return rendered.image().resize((width, height))

def make_color(color):
    """ Convert colors expressed as HTML-style RGB(A) strings to Blit.Color.
        
        Examples:
          white: "#ffffff", "#fff", "#ffff", "#ffffffff"
          black: "#000000", "#000", "#000f", "#000000ff"
          null: "#0000", "#00000000"
          orange: "#f90", "#ff9900", "#ff9900ff"
          transparent orange: "#f908", "#ff990088"
    """
    if type(color) not in (str, unicode):
        raise Core.KnownUnknown('Color must be a string: %s' % repr(color))

    if color[0] != '#':<commit_msg>Added support for blend modes in Sandwich provider<commit_after>from . import Core

from Blit import Color, Bitmap, blends

blend_modes = {
    'screen': blends.screen,
    'add': blends.add,
    'multiply': blends.multiply,
    'subtract': blends.subtract,
    'linear light': blends.linear_light,
    'hard light': blends.hard_light
    }

class Provider:
    """
    """
    def __init__(self, layer, stack):
        self.config = layer.config
        self.stack = stack
    
    def renderTile(self, width, height, srs, coord):
        
        # start with an empty base
        rendered = Color(0, 0, 0, 0x10)
        
        # a place to put rendered tiles
        tiles = dict()
        
        for layer in self.stack:
            #
            # Prepare pixels from elsewhere.
            #
            
            source_name, mask_name, color_name = [layer.get(k, None) for k in ('src', 'mask', 'color')]
        
            if source_name and color_name and mask_name:
                raise Core.KnownUnknown("You can't specify src, color and mask together in a Sandwich Layer: %s, %s, %s" % (repr(source_name), repr(color_name), repr(mask_name)))
            
            if source_name and source_name not in tiles:
                provider = self.config.layers[source_name].provider
                tiles[source_name] = Bitmap(provider.renderTile(width, height, srs, coord))
            
            if mask_name and mask_name not in tiles:
                provider = self.config.layers[mask_name].provider
                tiles[mask_name] = Bitmap(provider.renderTile(width, height, srs, coord))
            
            #
            # Build up the foreground layer.
            #
            
            if source_name and color_name:
                # color first, then layer
                foreground = make_color(color_name).blend(tiles[source_name])
            
            elif source_name:
                foreground = tiles[source_name]
            
            elif color_name:
                foreground = make_color(color_name)
    
            elif mask_name:
                raise Core.KnownUnknown("You have to provide more than just a mask to Sandwich Layer: %s" % repr(mask_name))
    
            else:
                raise Core.KnownUnknown("You have to provide at least some combination of src, color and mask to Sandwich Layer")
            
            #
            # Do the final composition.
            #
            
            kwargs = dict(opacity=float(layer.get('opacity', 1.0)))
            kwargs['blendfunc'] = blend_modes.get(layer.get('mode', None), None)
            
            if mask_name:
                rendered = rendered.blend(foreground, tiles[mask_name], **kwargs)
            
            else:
                rendered = rendered.blend(foreground, **kwargs)
    
        #
        
        if rendered.size() == (width, height):
            return rendered.image()
        else:
            return rendered.image().resize((width, height))

def make_color(color):
    """ Convert colors expressed as HTML-style RGB(A) strings to Blit.Color.
        
        Examples:
          white: "#ffffff", "#fff", "#ffff", "#ffffffff"
          black: "#000000", "#000", "#000f", "#000000ff"
          null: "#0000", "#00000000"
          orange: "#f90", "#ff9900", "#ff9900ff"
          transparent orange: "#f908", "#ff990088"
    """
    if type(color) not in (str, unicode):
        raise Core.KnownUnknown('Color must be a string: %s' % repr(color))

    if color[0] != '#':
{'fraction_non_alphanumeric': 0.1034994068801898, 'fraction_numerical': 0.01720047449584816, 'mean_word_length': 1.7142857142857142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '882600', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1801, 'n_words': 523}