def n_colors_over_value_range(starting_color=RGB.random_rgb(), n=random_number_of_colors(), value=random_range()): """ Returns n colors with same hue and saturation differing over a given value range. :param starting_color: color to start from :param n: number of colors to produce :param value: range of value to make colors from :return: list of colors of value range """ if n < 2: raise ValueError("n must be >= 2") elif value > 100: raise ValueError('Saturation range can\'t be > 100!') starting_color = starting_color.to_hsv() increment = value / (n - 1) cur_value = (HSV.max_sv / 2) - (value / 2) results = [] for idx in range(n): to_add = HSV(starting_color.hue, starting_color.saturation, cur_value) results.append(to_add.to_rgb()) cur_value += increment return results
def n_similar_colors(starting_color=RGB.random_rgb(), n=random_number_of_colors(), x=random_range()): """ Returns a list of n colors 'similar' to each other, or n colors even spaced out over the hue scale in the HSV color space, starting from a given RGB color. :param starting_color: color to start from :param n: number of colors to produce :param x: range of hue to make colors from :return: list of n similar colors """ starting_color = starting_color.to_hsv() hue = starting_color.hue start = (hue - x) % 360 n -= 1 if n < 2: increment = x * 2 else: increment = (x * 2) / (n + 1) cur_hue = start results = [] for idx in range(n + 1): to_add = HSV(cur_hue, starting_color.saturation, starting_color.value).to_rgb() results.append(to_add) cur_hue = (cur_hue + increment) % 360 return results
def n_evenly_spaced_colors(starting_color=RGB.random_rgb(), n=random_number_of_colors()): """ Returns a list of n colors evenly spaced out across the hue scale in the HSV color space from a given RGB color. :param starting_color: color to start from :param n: number of colors to include :return: """ results = [starting_color] starting_color = starting_color.to_hsv() increment = 360 / n n -= 1 cur_hue = starting_color.hue for new_color in range(n): cur_hue = (cur_hue + increment) % 360 to_add = HSV(cur_hue, starting_color.saturation, starting_color.value) results.append(to_add.to_rgb()) return results
def straight_granite(width, height): min_value = 0 max_value = 100 hsv = HSV.random_hsv() temp_height = height + 1 to_render = Image.new('HSV', (width, temp_height)) to_draw = ImageDraw.Draw(to_render) def mutation_value(): ending = 10 return random.randint(-ending, ending) def check_new_value(value): if value < min_value: return min_value elif value > max_value: return max_value return value for pixel in range(0, width): hsv.value = random.randint(min_value, max_value) to_draw.point((pixel, 0), fill=hsv.output()) for row in range(1, temp_height): for column in range(0, width): edge_value = 50 if column == 0: top_left_value = edge_value else: top_left_value = to_render.getpixel((column - 1, row - 1))[2] top_value = to_render.getpixel((column, row - 1))[2] if column == (width - 1): top_right_value = edge_value else: top_right_value = to_render.getpixel((column + 1, row - 1))[2] new_value = (top_left_value + top_value + top_right_value) / 3 new_value += mutation_value() new_value = check_new_value(new_value) hsv.value = new_value to_draw.point((column, row), fill=hsv.output()) to_render = to_render.crop((0, 1, width, temp_height)) return to_render
def granite(width, height): min_value = 0 max_value = 100 hsv = HSV.random_hsv() to_render = Image.new('HSV', (width, height)) to_draw = ImageDraw.Draw(to_render) def mutation_value(): ending = 10 return random.randint(-ending, ending) def check_new_value(value): if value < min_value: return min_value elif value > max_value: return max_value return value # Set value for starting pixel in upper left corner corner_start_value = random.randint(min_value, max_value) hsv.value = corner_start_value to_draw.point((0, 0), fill=hsv.output()) for pixel in range(1, width): new_value = hsv.value + mutation_value() new_value = check_new_value(new_value) hsv.value = new_value to_draw.point((pixel, 0), fill=hsv.output()) hsv.value = to_render.getpixel((0, 0))[2] for pixel in range(1, height): new_value = hsv.value + mutation_value() new_value = check_new_value(new_value) hsv.value = new_value to_draw.point((0, pixel), fill=hsv.output()) for row in range(1, height): for column in range(1, width): left_value = to_render.getpixel((column - 1, row))[2] bottom_value = to_render.getpixel((column, row - 1))[2] avg_value = round((left_value + bottom_value) / 2) new_value = avg_value + mutation_value() new_value = check_new_value(new_value) hsv.value = new_value to_draw.point((column, row), fill=hsv.output()) return to_render
def create_color_gradient(list_of_colors, n, mode='rgb', alpha=False, reflect=False): x = len(list_of_colors) if n <= x - 1: raise ValueError('') original_n = n if reflect: math.ceil(n / 2) if x == 1: return [copy.copy(list_of_colors[0]) for i in range(n)] pairs = [] for i in range(x - 1): a = list_of_colors[i] b = list_of_colors[i + 1] key = (a, b) pairs.append(key) base, overhang = divmod(n - 1, x - 1) def rounded_value_at_t(start, end, t): return round(start + ((end - start) * t)) gradient_values = [] for idx, pair in enumerate(pairs): temp = base if overhang > 0: temp += 1 overhang -= 1 a = pair[0] b = pair[1] for i in range(temp): t = i / temp if mode == 'hsv': hsv_a = a.to_hsv() hsv_b = b.to_hsv() to_add_hue = rounded_value_at_t(hsv_a.hue, hsv_b.hue, t) to_add_saturation = rounded_value_at_t(hsv_a.saturation, hsv_b.saturation, t) to_add_value = rounded_value_at_t(hsv_a.value, hsv_b.value, t) to_add = HSV(to_add_hue, to_add_saturation, to_add_value) to_add = to_add.to_rgb() elif mode == 'lab': lab_a = a.to_lab() lab_b = b.to_lab() to_add_light = rounded_value_at_t(lab_a.light, lab_b.light, t) to_add_a = rounded_value_at_t(lab_a.a, lab_b.a, t) to_add_b = rounded_value_at_t(lab_a.b, lab_b.b, t) to_add = LAB(to_add_light, to_add_a, to_add_b) to_add = to_add.to_rgb() elif mode == 'rgb': to_add_red = rounded_value_at_t(a.red, b.red, t) to_add_green = rounded_value_at_t(a.green, b.green, t) to_add_blue = rounded_value_at_t(a.blue, b.blue, t) to_add = RGB(to_add_red, to_add_green, to_add_blue) else: raise ValueError('Invalid color space!') if alpha: to_add.alpha = rounded_value_at_t(a.alpha, b.alpha, t) gradient_values.append(to_add.output_as_rgba()) else: gradient_values.append(to_add.output()) if idx == len(pairs) - 1 and i == temp - 1: if alpha: gradient_values.append(b.output_as_rgba()) else: gradient_values.append(b.output()) if reflect: gradient_values += gradient_values[::-1] if len(gradient_values) > original_n: gradient_values = gradient_values[:len(gradient_values) - 1] return gradient_values