Kkit.color

  1import requests
  2from haishoku.haishoku import Haishoku
  3import numpy as np
  4import colorsys
  5
  6def hex_to_rgb(value):
  7    """
  8    Convert a hex color string to an RGB tuple.
  9
 10    Parameters
 11    ----------
 12    value : str
 13        hex color string
 14
 15    Returns
 16    -------
 17    tuple
 18        RGB color tuple
 19    """
 20    value = value.lstrip('#')
 21    lv = len(value)
 22    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
 23
 24def rgb_to_hex(rgb):
 25    """
 26    Convert an RGB tuple to a hex color string.
 27
 28    Parameters
 29    ----------
 30    rgb : tuple
 31        RGB color tuple
 32
 33    Returns
 34    -------
 35    str
 36        hex color string
 37    """
 38    return '#%02x%02x%02x' % rgb
 39
 40def __normalize_rgb_color(color):
 41    return tuple([i/255 for i in color])
 42
 43def __denormalize_rgb_color(color):
 44    return tuple([i*255 for i in color])
 45
 46def rgb2hsv(color):
 47    """
 48    Convert an RGB color to an HSV color.
 49
 50    Parameters
 51    ----------
 52    color : tuple
 53        RGB color tuple
 54
 55    Returns
 56    -------
 57    tuple
 58        HSV color tuple
 59    """
 60    color = __normalize_rgb_color(color)
 61    color = colorsys.rgb_to_hsv(*color)
 62    return color
 63
 64def __int_rgb(color):
 65    return tuple([int(i) for i in color])
 66
 67def hsv2rgb(color):
 68    """
 69    Convert an HSV color to an RGB color.
 70
 71    Parameters
 72    ----------
 73    color : tuple
 74        HSV color tuple
 75
 76    Returns
 77    -------
 78    tuple
 79        RGB color tuple
 80    """
 81    color = colorsys.hsv_to_rgb(*color)
 82    color = __denormalize_rgb_color(color)
 83    color = __int_rgb(color)
 84    return color
 85
 86def str2color(string, Azure_key, num=5, verbose=False, vi=True):
 87    """
 88    Convert a string to a color.
 89
 90    This function uses the Bing Image Search API to search for images related to the input string, and then extracts the dominant color from the images to generate a color.
 91
 92    Parameters
 93    ----------
 94    string : str
 95        input string
 96
 97    Azure_key : str
 98        Microsoft Azure API key
 99
100    num : int
101        number of images to search
102        
103    verbose : bool
104        print the dominant color of each image
105
106    Returns
107    -------
108    tuple
109        RGB color tuple
110    list
111        list of RGB color tuples for each image
112    """
113    if num>150:
114        raise Exception("num must small than 150")
115    search_url = "https://api.bing.microsoft.com/v7.0/images/search"
116    headers = {"Ocp-Apim-Subscription-Key" : Azure_key}
117    # headers2 = {"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"}
118    params  = {"q": string}
119    response = requests.get(search_url, headers=headers, params=params)
120    response.raise_for_status()
121    search_results = response.json()
122    thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"]]
123    color_list = []
124    for i in range(num):
125        # time.sleep(0.5)
126        try:
127            # img_data = requests.get(thumbnail_urls[i], headers=headers2)
128            # do_color = np.array(Haishoku.getDominant(io.BytesIO(img_data.content)))
129            do_color = np.array(Haishoku.getDominant(thumbnail_urls[i]))
130            color_list.append(do_color)
131            if verbose:
132                print(i, thumbnail_urls[i], do_color)
133        except:
134            print("error: %s"%thumbnail_urls[i])
135    color_list = np.vstack(color_list)
136    rgb_color = np.mean(color_list, axis=0)
137    rgb_color = rgb_color.astype("int")
138    rgb_color = tuple(rgb_color)
139    hsl_color = rgb2hsv(rgb_color)
140    hsl_color = (hsl_color[0], 1, 0.7)
141    modified_rgb = hsv2rgb(hsl_color)
142    
143    if vi:
144         pass       
145
146    return modified_rgb, color_list
def hex_to_rgb(value):
 7def hex_to_rgb(value):
 8    """
 9    Convert a hex color string to an RGB tuple.
10
11    Parameters
12    ----------
13    value : str
14        hex color string
15
16    Returns
17    -------
18    tuple
19        RGB color tuple
20    """
21    value = value.lstrip('#')
22    lv = len(value)
23    return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))

Convert a hex color string to an RGB tuple.

Parameters

value : str hex color string

Returns

tuple RGB color tuple

def rgb_to_hex(rgb):
25def rgb_to_hex(rgb):
26    """
27    Convert an RGB tuple to a hex color string.
28
29    Parameters
30    ----------
31    rgb : tuple
32        RGB color tuple
33
34    Returns
35    -------
36    str
37        hex color string
38    """
39    return '#%02x%02x%02x' % rgb

Convert an RGB tuple to a hex color string.

Parameters

rgb : tuple RGB color tuple

Returns

str hex color string

def rgb2hsv(color):
47def rgb2hsv(color):
48    """
49    Convert an RGB color to an HSV color.
50
51    Parameters
52    ----------
53    color : tuple
54        RGB color tuple
55
56    Returns
57    -------
58    tuple
59        HSV color tuple
60    """
61    color = __normalize_rgb_color(color)
62    color = colorsys.rgb_to_hsv(*color)
63    return color

Convert an RGB color to an HSV color.

Parameters

color : tuple RGB color tuple

Returns

tuple HSV color tuple

def hsv2rgb(color):
68def hsv2rgb(color):
69    """
70    Convert an HSV color to an RGB color.
71
72    Parameters
73    ----------
74    color : tuple
75        HSV color tuple
76
77    Returns
78    -------
79    tuple
80        RGB color tuple
81    """
82    color = colorsys.hsv_to_rgb(*color)
83    color = __denormalize_rgb_color(color)
84    color = __int_rgb(color)
85    return color

Convert an HSV color to an RGB color.

Parameters

color : tuple HSV color tuple

Returns

tuple RGB color tuple

def str2color(string, Azure_key, num=5, verbose=False, vi=True):
 87def str2color(string, Azure_key, num=5, verbose=False, vi=True):
 88    """
 89    Convert a string to a color.
 90
 91    This function uses the Bing Image Search API to search for images related to the input string, and then extracts the dominant color from the images to generate a color.
 92
 93    Parameters
 94    ----------
 95    string : str
 96        input string
 97
 98    Azure_key : str
 99        Microsoft Azure API key
100
101    num : int
102        number of images to search
103        
104    verbose : bool
105        print the dominant color of each image
106
107    Returns
108    -------
109    tuple
110        RGB color tuple
111    list
112        list of RGB color tuples for each image
113    """
114    if num>150:
115        raise Exception("num must small than 150")
116    search_url = "https://api.bing.microsoft.com/v7.0/images/search"
117    headers = {"Ocp-Apim-Subscription-Key" : Azure_key}
118    # headers2 = {"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"}
119    params  = {"q": string}
120    response = requests.get(search_url, headers=headers, params=params)
121    response.raise_for_status()
122    search_results = response.json()
123    thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"]]
124    color_list = []
125    for i in range(num):
126        # time.sleep(0.5)
127        try:
128            # img_data = requests.get(thumbnail_urls[i], headers=headers2)
129            # do_color = np.array(Haishoku.getDominant(io.BytesIO(img_data.content)))
130            do_color = np.array(Haishoku.getDominant(thumbnail_urls[i]))
131            color_list.append(do_color)
132            if verbose:
133                print(i, thumbnail_urls[i], do_color)
134        except:
135            print("error: %s"%thumbnail_urls[i])
136    color_list = np.vstack(color_list)
137    rgb_color = np.mean(color_list, axis=0)
138    rgb_color = rgb_color.astype("int")
139    rgb_color = tuple(rgb_color)
140    hsl_color = rgb2hsv(rgb_color)
141    hsl_color = (hsl_color[0], 1, 0.7)
142    modified_rgb = hsv2rgb(hsl_color)
143    
144    if vi:
145         pass       
146
147    return modified_rgb, color_list

Convert a string to a color.

This function uses the Bing Image Search API to search for images related to the input string, and then extracts the dominant color from the images to generate a color.

Parameters

string : str input string

Azure_key : str Microsoft Azure API key

num : int number of images to search

verbose : bool print the dominant color of each image

Returns

tuple RGB color tuple list list of RGB color tuples for each image