Example #1
0
    def twinkle_simplex(self, idx):
        nv = raw_noise_2d(self.noise_array[idx],
                          random.random()) / self.options.get('simplex_damper', 4.0)
        self.noise_array[idx] += nv
        if self.noise_array[idx] > 1.0:
            self.noise_array[idx] = 1.0
            pass
        elif self.noise_array[idx] < -1.0:
            self.noise_array[idx] = -1.0
            pass

        ranger = (self.noise_array[idx] + 1.0) / 2.0

        # Adjust colour. 
        (base_r, base_g, base_b) = self.options['init_pattern'][idx]
        #log.debug("adjusting from orig: %d %d %d", base_r, base_g, base_b)
        r = int(base_r * ranger)
        g = int(base_g * ranger)
        b = int(base_b * ranger)
        self.hol.setglobe(idx, r, g, b)
        pass
Example #2
0
def twinkle_holiday(hol, options, init_pattern, noise_array=None):
    """
    Make a Holiday twinkle like the stars
    """
    # For each globe, mostly have a random drift of brightness
    # and hue by but occasionally jump in brightness up or down
    if options.basecolor:
        (base_r, base_g, base_b) = webcolors.hex_to_rgb(options.basecolor)
        base_hsv = colorsys.rgb_to_hsv(base_r/255.0, base_g/255.0, base_b/255.0)
        pass
    
    if noise_array is None:
        noise_array = [ 0, ] * HolidaySecretAPI.NUM_GLOBES
        pass

    if options.twinkle_algo == 'throb':

        # Increase brightness by throb_speed / anim_sleep
        # until max brightness, and then decrease
        r, g, b = hol.getglobe(0)
        (h, s, v) = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)

        # Check direction for throb. The limits are set to be visually
        # interesting, because value above about 0.6 doesn't look much
        # different.
        if v > 0.6:
            options.throb_dir = False
        elif v < 0.02:
            options.throb_dir = True

        throb_amount = (1.0 / options.throb_speed * options.anim_sleep)
        for idx in range(0, HolidaySecretAPI.NUM_GLOBES):
            r, g, b = hol.getglobe(idx)
            (h, s, v) = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
            if options.throb_dir:
                v += throb_amount
                if v > 0.91:
                    v = 0.91
            else:
                v -= throb_amount
                if v < 0.02:
                    v = 0.02
                pass
            (r, g, b) = colorsys.hsv_to_rgb(h, s, v)
            hol.setglobe(idx, int(255*r), int(255*g), int(255*b))
            pass
        pass

    elif options.twinkle_algo in ['simplex', 'random']:
    
        for idx in range(0, HolidaySecretAPI.NUM_GLOBES):

            # Choose globe update algorithm
            if options.twinkle_algo == 'simplex':
                nv = raw_noise_2d(noise_array[idx], random.random()) / options.simplex_damper
                noise_array[idx] += nv
                if noise_array[idx] > 1.0:
                    noise_array[idx] = 1.0
                    pass
                elif noise_array[idx] < -1.0:
                    noise_array[idx] = -1.0
                    pass

                ranger = (noise_array[idx] + 1.0) / 1.0
                #log.debug("ranger: %f", ranger)
                # Adjust colour. If basecolor, adjust from basecolor
                if options.basecolor:
                    (base_r, base_g, base_b) = webcolors.hex_to_rgb(options.basecolor)
                    #log.debug("adjusting from base: %d %d %d", base_r, base_g, base_b)
                    r = int(base_r * ranger)
                    g = int(base_g * ranger)
                    b = int(base_b * ranger)
                    pass
                else:
                    # adjust from original color
                    (base_r, base_g, base_b) = init_pattern[idx]
                    #log.debug("init pattern: %s", init_pattern[idx])
                    #log.debug("adjusting from orig: %d %d %d", base_r, base_g, base_b)
                    r = int(base_r * ranger)
                    #log.debug("adjusted red from orig: %d %d %d", base_r, base_g, base_b)
                    g = int(base_g * ranger)
                    b = int(base_b * ranger)
                    pass

                if r > 255:
                    r = 255
                if g > 255:
                    g = 255
                if b > 255:
                    b = 255
                    
                hol.setglobe(idx, r, g, b)
                #log.debug("init pattern: %s", init_pattern[idx])
            else:
                # % chance of updating a given globe
                if random.random() < options.change_chance:

                    r, g, b = hol.getglobe(idx)
                    (h, s, v) = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
                    #log.debug("start h s v: %f %f %f", h, s, v)
                    # Adjust hue by a random amount
                    huestep = random.random() * options.huestep_max
                    # 50% chance of positive or negative step
                    if random.randint(0, 1):
                        h += huestep
                        if options.basecolor and abs(base_hsv[0] - h) > options.huediff_max:
                            h = base_hsv[0] + options.huediff_max
                        if h > 1.0:
                            h = h - 1.0
                    else:
                        h -= huestep
                        if options.basecolor and abs(h - base_hsv[0]) > options.huediff_max:
                            h = base_hsv[0] - options.huediff_max

                        if h < 0.0:
                            h = 1.0 + h
                        pass

                    satstep = random.random() * options.satstep_max
                    if random.randint(0, 1):
                        s += satstep
                        if options.basecolor and abs(base_hsv[1] - s) > options.satdiff_max:
                            s = base_hsv[1] + options.satdiff_max

                        if s > 1.0:
                            s = 1.0
                    else:
                        s -= satstep
                        if options.basecolor and abs(s - base_hsv[1]) > options.satdiff_max:
                            s = base_hsv[1] - options.satdiff_max

                        # Make sure things stay bright and colorful!
                        if s < 0.0:
                            s = 0.0

                    # Adjust value by a random amount
                    valstep = random.random() * options.valstep_max
                    # 50% chance of positive or negative step
                    if random.randint(0, 1):
                        v += valstep
                        if options.basecolor and abs(base_hsv[2] - v) > options.valdiff_max:
                            v = base_hsv[2] + options.valdiff_max

                        if v > 1.0:
                            v = 1.0
                    else:
                        v -= valstep
                        if options.basecolor and abs(v - base_hsv[2]) > options.valdiff_max:
                            v = base_hsv[2] - options.valdiff_max

                        if v < 0.2:
                            v = 0.2
                        pass

                    #log.debug("end h s v: %f %f %f", h, s, v)

                    (r, g, b) = colorsys.hsv_to_rgb(h, s, v)
                    #log.debug("r g b: %f %f %f", r, g, b)
                    hol.setglobe(idx, int(255*r), int(255*g), int(255*b))
                    pass
                pass
            pass
        pass
    elif options.twinkle_algo in ['chase']:
        # Chase mode?
        if options.chase:
            # Rotate all globes around by one place
            oldglobes = hol.globes[:]
            hol.globes = oldglobes[1:]
            hol.globes.append(oldglobes[0])
            pass
        else:
            #log.debug("old: %s", hol.globes)
            oldglobes = hol.globes[:]
            hol.globes = oldglobes[:-1]
            hol.globes.insert(0, oldglobes[-1])
            #log.debug("new: %s", hol.globes)
            pass
    
    hol.render()
Example #3
0
File: rings.py Project: zrlram/leds
 def noise(x, spin=0.01):
     return simplexnoise.raw_noise_2d(x, x * spin) * 0.5 + 0.5