Apr 8

Mar 10

Color Palette in Python

I though this might be of use to somebody. It got some rough edges but is good enough™ for my needs.

# -*- coding: utf-8 -*-
from colorsys import rgb_to_hls, hls_to_rgb
from PIL import Image
import struct
from cStringIO import StringIO

__author__ = 'Simon Pantzare'

class Color(object):
    def __init__(self, r, g, b):
        self.r = r
        self.g = g
        self.b = b
        self.h, self.l, self.s = rgb_to_hls(r/255.0, g/255.0, b/255.0)
        self.h *= 360
        self.l *= 100
        self.s *= 100


class RangeImage(object):
    def __init__(
            self, 
            start_rgb, 
            lightness_pc, 
            hue_dg,
            dim=(200,50)
            ):
        """
        lightness_pc 
            Range 0-50%.

        hue_dg
            Range 0-180 degrees.
        """
        self.start_color = Color(*start_rgb)
        self.lightness_pc = lightness_pc
        self.hue_dg = hue_dg
        self.width_px = dim[0]
        self.height_px = dim[1]

    def h_vector(self):
        assert 0 <= self.hue_dg and self.hue_dg <= 180
        start_h = self.start_color.h - self.hue_dg
        if start_h < 0:
            start_h = 360 + start_h
        incr = float(self.hue_dg*2) / self.height_px
        return [(start_h+i*incr)%(360+1) for i in range(0, self.height_px)]

    def l_vector(self):
        assert 0 <= self.lightness_pc and self.lightness_pc <= 50
        minv = max(self.start_color.l - self.lightness_pc, 0)
        incr = float(self.lightness_pc*2) / self.width_px
        return [min(minv+i*incr,100) for i in range(0, self.width_px)]

    def image(self):
        arr = [[0] * self.width_px for _ in range(self.height_px)] # the outer list comprehension IS needed
        hues = self.h_vector()
        ls = self.l_vector()
        sat = self.start_color.s / 100

        for row_idx, row in enumerate(arr):
            h = hues[row_idx] / 360.0
            for col_idx, px in enumerate(row):
                l = ls[col_idx] / 100.0
                r,g,b = hls_to_rgb(h, l, sat)
                arr[row_idx][col_idx] = struct.pack(
                        "BBBB", 
                        int(r*255),
                        int(g*255),
                        int(b*255),
                        255
                        )

        buf = StringIO()
        [[buf.write(px) for px in row] for row in arr]
        buf.seek(0)

        im = Image.frombuffer(
                "RGBA", 
                (self.width_px, self.height_px), 
                buf.read(),
                "raw",
                "RGBA",
                0,
                1
                )
        return im

Output for RangeImage((0, 255, 0), 50, 180, (200, 100)).image():

RangeImage((0, 255, 0), 50, 180, (200, 100))


Feb 28
“The only difference between a bug and a feature is the documentation.” — Anonymous

Feb 2
“Facebook will take the bold step of moving entirely into the cloud and shutting down its own data centers. Later it will be discovered that the cloud-provider they chose had, themselves, moved entirely into the cloud and shut down their data centers. After a few more recursive steps, it’s learned that the guy at the end of the chain had moved his operation into the Facebook cloud, and the entire service is hosted on thin air.” yacoset prediction for 2010

Jan 7
&#8220;My grandfather called me over to his house for a &#8216;computer emergency&#8217; and gave me these blueprints for success.&#8221; — DefaultGen at reddit

“My grandfather called me over to his house for a ‘computer emergency’ and gave me these blueprints for success.” — DefaultGen at reddit


Jan 6
“I can’t help but think of homeopathy when I stumble upon some SEO-related crap.”

Dec 24
“The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says ‘Hmm. I think I’ve seen something like this before.’” John D. Cook

Dec 23
“The ultimate goal of computer science is to help produce better systems. Would you trust someone who had not seen a patient for years to teach surgery? What would you think of a piano teacher who never touched the keyboard? A CS education must bring a student beyond the necessary book learning to a mastery of its application in complete systems and an appreciation of aesthetics in code.” Bjarne Stroustrup

Nov 28
“You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.” — Joe Armstrong, creator of Erlang, on the problems with object-oriented languages and software reuse

Nov 25
&#8220;I think this summarizes everything.&#8221; — mnmal

“I think this summarizes everything.” — mnmal




Simon Pantzare
Simon Pantzare, also available on
facebook,
twitter,
reddit,
github.
Ask me anything.