def get_color_by_hex( hex_code: str, shades: int = 0, json: Optional[bool] = None, all: bool = False, units: Optional[bool] = None, ): """Look up colors by hexadecimal (web) code. The hexadecimal code must be specified without the hash (#) prefix and match the regular expression: ^(?:[0-9a-fA-F]{3}){1,2}$ Usage examples: color hex FFFFFF color hex FFF color hex FFFFFF --shades --json --all --units :param hex_code: Hex color code without the hash (#) prefix. :param shades: Display different shades of the specified color. :param json: Display in JSON format. :param all: Bypass user configuration and display all keys. :param units: Bypass user configuration and display units. """ config = load_config_file() config.set_flags( shades=validate_shades_count(shades), json=validate_boolean_flag(json), all=validate_boolean_flag(all), units=validate_boolean_flag(units), ) hex_code = normalize_hex_code(hex_code) print_color(config, Color(*hex_to_rgb(hex_code)))
def test_list_view(r, g, b): color = Color(r, g, b) view = format_list_view(default_config, color) assert len(view.split("|")) == len(default_config.list_view_keys) view = format_list_view(custom_config, color) assert len(view.split("|")) == len(custom_config.list_view_keys)
def test_color_to_dict(r, g, b): color = Color(r, g, b) assert color.get_dict(set()) == {} assert color.get_dict({"foo", "bar"}) == {} assert color.get_dict({"rgb"}) == {"rgb": (r, g, b)} keys = {"hex", "rgb", "hsl", "hsv", "cmyk", "name", "is_name_exact"} assert set(color.get_dict(keys).keys()) == keys
def test_color_white(): color = Color(255, 255, 255) assert color.name == "white" assert color.names == ("white", ) assert color.is_name_exact is True assert color.hex == "FFFFFF" assert color.cmyk == (0, 0, 0, 0) assert color.rgb == (255, 255, 255) assert color.hsl == (0.0, 0.0, 1.0) assert color.hsv == (0.0, 0.0, 1.0)
def test_color_black(): color = Color(0, 0, 0) assert color.name == "black" assert color.names == ("black", ) assert color.is_name_exact is True assert color.hex == "000000" assert color.cmyk == (0, 0, 0, 1) assert color.rgb == (0, 0, 0) assert color.hsl == (0, 0, 0) assert color.hsv == (0, 0, 0)
def test_color_dimgray(): color = Color(100, 100, 100) assert color.name == "dimgray/dimgrey" assert color.names == ("dimgray", "dimgrey") assert color.is_name_exact is False assert color.hex == "646464" assert color.cmyk == (0.0, 0.0, 0.0, 0.607843137254902) assert color.rgb == (100, 100, 100) assert color.hsl == (0.0, 0.0, 0.39215686274509803) assert color.hsv == (0.0, 0.0, 0.39215686274509803)
def function( json: Optional[bool] = None, all: bool = False, units: Optional[bool] = None, ): config = load_config_file() config.set_flags( json=validate_boolean_flag(json), all=validate_boolean_flag(all), units=validate_boolean_flag(units), ) print_colors(config, [Color(*rgb) for rgb in palette_to_rgbs(name)])
def function( shades: int = 0, json: Optional[bool] = None, all: bool = False, units: Optional[bool] = None, ): config = load_config_file() config.set_flags( shades=validate_shades_count(shades), json=validate_boolean_flag(json), all=validate_boolean_flag(all), units=validate_boolean_flag(units), ) print_color(config, Color(*name_to_rgb(name)))
def test_get_view(r, g, b): color = Color(r, g, b) view = format_get_view(default_config, color) assert "Name" in view assert "Hex" in view assert "RGB" in view assert "HSL" in view assert "HSV" in view assert "CMYK" in view view = format_get_view(custom_config, color) assert "Name" in view assert "Hex" in view assert "RGB" not in view assert "HSL" not in view assert "HSV" not in view assert "CMYK" not in view
def get_color_by_cmyk( c: float, m: float, y: float, k: float, shades: int = 0, json: Optional[bool] = None, all: bool = False, units: Optional[bool] = None, ): """Look up colors by CMYK (Cyan Magenta Yellow Black) values. CMYK is a subtractive color model used in color printing. It refers to the four ink plates used in color printing: cyan, magenta, yellow, and key (black). Values must be between 0.0 and 100.0 inclusive. Usage examples: color cmyk 100 100 100 100 color cmyk 0 0 0 0 color cmyk 10 20 30 40 --shades --json --all --units :param c: Cyan % (0.0 to 100.0 inclusive). :param m: Magenta % (0.0 to 100.0 inclusive). :param y: Yellow % (0.0 to 100.0 inclusive). :param k: Black/Key % (0.0 to 100.0 inclusive). :param shades: Display different shades of the specified color. :param json: Display in JSON format. :param all: Bypass user configuration and display all keys. :param units: Bypass user configuration and display units. """ config = load_config_file() config.set_flags( shades=validate_shades_count(shades), json=validate_boolean_flag(json), all=validate_boolean_flag(all), units=validate_boolean_flag(units), ) c = normalize_percent_value(c) m = normalize_percent_value(m) y = normalize_percent_value(y) k = normalize_percent_value(k) print_color(config, Color(*cmyk_to_rgb(c, m, y, k)))
def get_color_by_hsv( h: float, s: float, v: float, shades: int = 0, json: Optional[bool] = None, all: bool = False, units: Optional[bool] = None, ): """Look up colors by HSV (Hue Saturation Brightness/Value) values. Hue is a point on the color wheel from 0 to 360 where 0 is red, 120 is green, and 240 is blue. Saturation is a percentage value where 0% means faded, and 100% means full color. Brightness (or value) is a percentage value where 0% is black and 100% reveals the most color. Usage examples: color hsv 360 100 100 color hsv 0 0 0 color hsv 10 20 30 --shades --json --all --units :param h: Hue in degree angle (0.0 to 360.0 inclusive). :param s: Saturation % (0.0 to 100.0 inclusive). :param v: Brightness/Value % (0.0 to 100.0 inclusive). :param shades: Display different shades of the specified color. :param json: Display in JSON format. :param all: Bypass user configuration and display all keys. :param units: Bypass user configuration and display units. """ config = load_config_file() config.set_flags( shades=validate_shades_count(shades), json=validate_boolean_flag(json), all=validate_boolean_flag(all), units=validate_boolean_flag(units), ) h = normalize_degree_angle(h) s = normalize_percent_value(s) v = normalize_percent_value(v) print_color(config, Color(*hsv_to_rgb(h, s, v)))
def get_color_by_rgb( r: int, g: int, b: int, shades: int = 0, json: Optional[bool] = None, all: bool = False, units: Optional[bool] = None, ): """Look up colors by RGB (Red Green Blue) values. RGB is an additive color model where red, green, and blue lights are superimposed together to yield shades of colors. The RGB values must be integers between 0 to 255. Usage examples: color rgb 255 255 255 color rgb 0 0 0 color rgb 10 20 30 --shades --json --all --units :param r: Red (0 to 255 inclusive). :param g: Green (0 to 255 inclusive). :param b: Blue (0 to 255 inclusive). :param shades: Display different shades of the specified color. :param json: Display in JSON format. :param all: Bypass user configuration and display all keys. :param units: Bypass user configuration and display units. """ config = load_config_file() config.set_flags( shades=validate_shades_count(shades), json=validate_boolean_flag(json), all=validate_boolean_flag(all), units=validate_boolean_flag(units), ) r = validate_rgb_value(r) g = validate_rgb_value(g) b = validate_rgb_value(b) print_color(config, Color(r, g, b))
def test_color_darkslateblue(): color = Color(50, 100, 150) assert color.name == "darkslateblue" assert color.names == ("darkslateblue", ) assert color.is_name_exact is False assert color.hex == "326496" assert color.cmyk == ( 0.6666666666666667, 0.3333333333333335, 0.0, 0.4117647058823529, ) assert color.rgb == (50, 100, 150) assert color.hsl == ( 0.5833333333333334, 0.5000000000000001, 0.39215686274509803, ) assert color.hsv == ( 0.5833333333333334, 0.6666666666666667, 0.5882352941176471, )
def test_color_get_shades(r, g, b, shades_count): color = Color(r, g, b) colors = set(color.get_shades(shades_count)) assert len(colors) == shades_count assert shades_count == 1 or Color(0, 0, 0) in colors assert shades_count == 1 or Color(255, 255, 255) in colors