Beispiel #1
0
 def test_parser_no_ending_slash(self):
     parser = palette.PaletteParser()
     with self.assertRaises(errors.PaletteParseError) as cm:
         parser.parse('P/0f-01-02-03')
     self.assertEqual(cm.exception.i, 13)
     self.assertTrue('Expected: "/"' in cm.exception.msg)
     self.assertTrue((' ' * 13 + '^') in str(cm.exception))
Beispiel #2
0
 def test_parser_bad_hex(self):
     parser = palette.PaletteParser()
     with self.assertRaises(errors.PaletteParseError) as cm:
         parser.parse('P/0f-01-4g/')
     self.assertEqual(cm.exception.i, 8)
     self.assertTrue('Invalid hex value' in cm.exception.msg)
     self.assertTrue((' ' * 8 + '^') in str(cm.exception))
Beispiel #3
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
Beispiel #5
0
 def test_parser(self):
     parser = palette.PaletteParser()
     pal = parser.parse('P/0f-01-02/')
     self.assertEqual(str(pal), 'P/0f-01-02/')
     pal = parser.parse('P/0f-01-02-03/0f-04-05-06/0f-10/')
     self.assertEqual(str(pal), 'P/0f-01-02-03/0f-04-05-06/0f-10/')
Beispiel #6
0
 def test_parser_zero_is_valid(self):
     parser = palette.PaletteParser()
     pal = parser.parse('P/0f-00/')
     self.assertEqual(str(pal), 'P/0f-00/')