"""This module provides plugins for NEU metrics."""

import kurt
from hairball.plugins import HairballPlugin


class Variables(HairballPlugin):

    """Plugin that counts the number of variables in a project."""

    def __init__(self):
        super(Variables, self).__init__()
        self.total = 0

    def finalize(self):
        """Output the number of variables in the project."""
        print("Number of variables: %i" % self.total)

    def analyze(self, scratch):
        """Run and return the results of the Variables plugin."""          
        self.total = len(scratch.variables)
        for x in scratch.sprites:
            self.total += len(x.variables)

class Lists(HairballPlugin):

    """Plugin that counts the number of lists in a project."""

    def __init__(self):
        super(Lists, self).__init__()
        self.total = 0

    def finalize(self):
        """Output the number of lists in the project."""
        print("Number of lists: %i" % self.total)

    def analyze(self, scratch):
        """Run and return the results of the Lists plugin."""
        self.total = len(scratch.lists)
        for x in scratch.sprites:
            self.total += len(x.lists)

class BlockCounts(HairballPlugin):

    """Plugin that keeps track of the number of blocks in a project."""

    def __init__(self):
        super(BlockCounts, self).__init__()
        self.blocks = 0

    def finalize(self):
        """Output the aggregate block count results."""
        print("Number of blocks %i" % self.blocks)

    def analyze(self, scratch):
        """Run and return the results from the BlockCounts plugin."""
        for script in self.iter_scripts(scratch):
            for b in self.iter_blocks(script.blocks):
                self.blocks += 1


@@ -102,16 +102,48 @@
 lPlugin%0A
+from PIL import Image%0Aimport os%0A
 %0A%0Aclass 
@@ -1792,8 +1792,1147 @@
 s += 1%0A%0A
+class Colors(HairballPlugin):%0A%0A    %22%22%22Plugin that keeps track of the colors of the stage images.%22%22%22%0A%0A    def __init__(self):%0A        self.colors =%7B%7D%0A%0A    def finalize(self):%0A        %22%22%22Output the aggregate block count results.%22%22%22%0A        print self.colors%0A%0A    def compute_average_image_color(self, img):%0A        %22%22%22%0A            Compute the most frequent color in img.%0A            Code adapted from %0A            http://blog.zeevgilovitz.com/detecting-dominant-colours-in-python/%0A        %22%22%22%0A        image = Image.open(img)%0A        w, h = image.size%0A        pixels = image.getcolors(w * h)%0A        most_frequent_pixel = pixels%5B0%5D%0A        for count, colour in pixels:%0A            if count %3E most_frequent_pixel%5B0%5D:%0A                most_frequent_pixel = (count, colour)%0A        rgb = %5B%5D%0A        for i in range(3):%0A            rgb.append (most_frequent_pixel%5B1%5D%5Bi%5D)%0A        trgb = tuple(rgb)%0A        trgb = '#%2502x%2502x%2502x' %25 trgb #Transform rgb to Hex color (HTML)%0A        return trgb%0A%0A    def analyze(self, scratch):%0A        %22%22%22Run and return the results from the BlockCounts plugin.%22%22%22%0A        #ToDo: get the images from stage and characters

Add Colors class