def gradient(length: int, *colors) -> list: """ Generate a looped gradient from multiple evenly-spaced colors Uses the new HSLUV colorspace :param length: Total number of entries in the final gradient :param colors: Color stops, varargs :return: List of colors in the gradient """ luv_colors = [rgb_to_hsluv(to_color(x).rgb) for x in colors] luv_colors.append(luv_colors[0]) steps = max(len(luv_colors), math.floor(length / (len(luv_colors)))) gradient = [] for color_idx in range(0, len(luv_colors) - 1): start = luv_colors[color_idx] end = luv_colors[(color_idx + 1)] for interp in range(0, steps): amount = float(interp) / float(steps) i = ColorUtils._circular_interp(start, end, amount) gradient.append( Color.NewFromRgb(*hsluv_to_rgb([i[0], i[1], i[2]]))) return gradient
def darken(rgb, amount=.15): """ Darken a color by the given amount in HSLuv space. """ h, s, l = hsluv.rgb_to_hsluv(rgb) return hsluv.hsluv_to_rgb((h, s, (1 - amount)*l))
def perturb(self, img): raw = img_to_matrix(img) perturbed = raw / 255.0 w, h, c = perturbed.shape for i in range(w): for j in range(h): px_color = perturbed[i, j] hsl = np.array(rgb_to_hsluv(px_color)) hsl *= self.mul hsl[0] = np.clip(hsl[0], 0, 360) hsl[1:] = np.clip(hsl[1:], 0, 100) perturbed[i, j] = hsluv_to_rgb(hsl) perturbed *= 255.0 raw_img = Image.fromarray(raw.astype("uint8"), "RGB") perturbed_img = Image.fromarray(perturbed.astype("uint8"), "RGB") noise_img = ImageChops.difference(raw_img, perturbed_img) noise_img = Image.fromarray(img_to_matrix(noise_img) * 50, "RGB") # noise_img = ImageOps.invert(noise_img.convert("L")).convert("RGB") # print((img_to_matrix(noise_img) * 10).max()) # noise_img = Image.fromarray(, "RGB") return raw_img, perturbed_img, noise_img
def convert_hex_color_strings_to_rgb_and_hsluv_arrays(color_hex_str_list): rgb_array_list = [] hsluv_array_list = [] for hex_color_str in color_hex_str_list: # Remove leading '#', if any hex_color_str = hex_color_str.lstrip("#") r = int(hex_color_str[0:2], 16) g = int(hex_color_str[2:4], 16) b = int(hex_color_str[4:6], 16) rgb_array = np.array([r, g, b], dtype="uint8").reshape((1, 1, 3)) rgb_array_list.append(rgb_array) hsluv_tuple = rgb_to_hsluv((r / 255.0, g / 255.0, b / 255.0)) hsluv_array = np.array( [hsluv_tuple[0], hsluv_tuple[1], hsluv_tuple[2]], dtype="float").reshape((1, 1, 3)) hsluv_array_list.append(hsluv_array) return rgb_array_list, hsluv_array_list
def _hsva(color: Color) -> Color: return (*rgb_to_hsluv(color.rgb), color.alpha)
def hsl(self): return hsluv.rgb_to_hsluv(self.rgb)
def channel(c, channel=0, amt=1): """Increment a rgb channel""" rgb = hsluv.hsluv_to_rgb(c) rgb[channel] = max(0, min(1, rgb[channel] + amt / 255)) return hsluv.rgb_to_hsluv(rgb)
def rgba(self, value: ColorTuple) -> None: r, g, b = (value[0] / 255, value[1] / 255, value[2] / 255) self.hsluva = rgb_to_hsluv((r, g, b)) + (self.alpha, )