Example #1
0
 def add(self, p):
     if self.bg_color is None:
         raise errors.PaletteBackgroundColorMissingError()
     elif p[0] == 0:
         pass
     elif self.bg_color != p[0]:
         raise errors.PaletteBackgroundColorConflictError(
             p[0], self.bg_color)
     p = [self.bg_color] + p[1:]
     self.pals.append(p)
     self.pal_as_sets.append(set(p))
Example #2
0
    def make_palette(self, palette_text, bg_color, is_sprite):
        """Construct the palette object from parsable text or color_needs.

    palette_text: Optional text to parse palette object from.
    bg_color: Background color. Must match palette's background color, if given.
    is_sprite: Whether this is a sprite palette.
    """
        pal = None
        if palette_text:
            # If palette argument was passed, use as either a literal or a file.
            try:
                if palette_text.startswith('P/'):
                    # Literal palette string.
                    parser = palette.PaletteParser()
                    pal = parser.parse(palette_text)
                elif os.path.isfile(palette_text):
                    # File containing a palette.
                    reader = palette.PaletteFileReader()
                    pal = reader.read(palette_text)
            except errors.PaletteParseError as e:
                self._err.add(e)
                return None
            if not bg_color is None and pal.bg_color != bg_color:
                self._err.add(
                    errors.PaletteBackgroundColorConflictError(
                        pal.bg_color, bg_color))
                return None
            return pal
        if self.img.palette:
            # If the image uses indexed color, try to extract a palette.
            extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
                self)
            pal = extractor.extract_palette(self.img.palette, self.img.format)
            if pal:
                return pal
        # If sprite mode, and there's no bg color, we can figure it out based upon
        # how many empty tiles there need to be.
        if is_sprite and bg_color is None and not self._test_only_auto_sprite_bg:
            for color, num in self._needs_provider.counts():
                if num >= 0x40 and bg_color is None:
                    bg_color = color
        # Make the palette from the color needs.
        guesser = guess_best_palette.GuessBestPalette()
        if not bg_color is None:
            guesser.set_bg_color(bg_color)
        color_sets = self._needs_provider.elems()
        try:
            pal = guesser.guess_palette(color_sets)
        except errors.TooManyPalettesError as e:
            self._err.add(e)
            return None
        return pal
    def parse_palette(self, palette_text, bg_color):
        """Parse the palette either from command-line flag, or indexed image.

    palette_text: Optional text to parse palette object from.
    bg_color: Background color. Must match palette's background color, if given.
    """
        pal = None
        if palette_text:
            # If palette argument was passed, parse it.
            try:
                if palette_text == '+':
                    # Attempt to extract the palette from the image, see below.
                    pass
                elif palette_text == '-':
                    # Do not attempt to extract the palette.
                    return None
                elif palette_text.startswith('P/'):
                    # Literal palette string.
                    parser = palette.PaletteParser()
                    pal = parser.parse(palette_text)
                elif os.path.isfile(palette_text):
                    # File containing a palette.
                    reader = palette.PaletteFileReader()
                    pal = reader.read(palette_text)
                else:
                    raise errors.PaletteParseError('Unknown palette "%s"' %
                                                   palette_text)
            except errors.PaletteParseError as e:
                self._err.add(e)
                return None
            if pal:
                if bg_color is not None and pal.bg_color != bg_color:
                    self._err.add(
                        errors.PaletteBackgroundColorConflictError(
                            pal.bg_color, bg_color))
                    return None
                return pal
        # If image uses indexed color, attempt to extract the palette.
        if self.img.palette:
            extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
                self)
            try:
                w = wrapped_image_palette.WrappedImagePalette.from_image(
                    self.img)
                pal = extractor.extract_palette(w)
            except errors.PaletteExtractionError as e:
                if palette_text is not None:
                    self._err.add(e)
                return None
            if pal:
                return pal
        return None