当前位置: 首页>>代码示例>>Python>>正文


Python colorsys.rgb_to_hls方法代码示例

本文整理汇总了Python中colorsys.rgb_to_hls方法的典型用法代码示例。如果您正苦于以下问题:Python colorsys.rgb_to_hls方法的具体用法?Python colorsys.rgb_to_hls怎么用?Python colorsys.rgb_to_hls使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在colorsys的用法示例。


在下文中一共展示了colorsys.rgb_to_hls方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: random_color

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def random_color():
    import random, colorsys
    
    red = (random.randrange(0, 256) + 256) / 2.0;
    green = (random.randrange(0, 256) + 256) / 2.0;
    blue = (random.randrange(0, 256) + 256) / 2.0;
    
    def to_hex(i):
        hx = hex(int(i))[2:]
        if len(hx) == 1:
            hx = "0" + hx
        return hx
    def to_color(r,g,b):
        cs = [to_hex(c) for c in [r,g,b]]
        return "#{cs[0]}{cs[1]}{cs[2]}".format(cs=cs)
    def darker(r,g,b):
        h,l,s = colorsys.rgb_to_hls(r/256.0, g/256.0, b/256.0)
        cs = [to_hex(256.0*c) for c in colorsys.hls_to_rgb(h,l/2.5,s)]
        return "#{cs[0]}{cs[1]}{cs[2]}".format(cs=cs)
    
    return {"background":to_color(red,green,blue),"foreground":darker(red,green,blue)} 
开发者ID:YoannDupont,项目名称:SEM,代码行数:23,代码来源:misc.py

示例2: test_hls_values

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def test_hls_values(self):
        values = [
            # rgb, hls
            ((0.0, 0.0, 0.0), (  0  , 0.0, 0.0)), # black
            ((0.0, 0.0, 1.0), (4./6., 0.5, 1.0)), # blue
            ((0.0, 1.0, 0.0), (2./6., 0.5, 1.0)), # green
            ((0.0, 1.0, 1.0), (3./6., 0.5, 1.0)), # cyan
            ((1.0, 0.0, 0.0), (  0  , 0.5, 1.0)), # red
            ((1.0, 0.0, 1.0), (5./6., 0.5, 1.0)), # purple
            ((1.0, 1.0, 0.0), (1./6., 0.5, 1.0)), # yellow
            ((1.0, 1.0, 1.0), (  0  , 1.0, 0.0)), # white
            ((0.5, 0.5, 0.5), (  0  , 0.5, 0.0)), # grey
        ]
        for (rgb, hls) in values:
            self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb))
            self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls)) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:18,代码来源:test_colorsys.py

示例3: test_include_can_measure_stdlib

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def test_include_can_measure_stdlib(self):
        self.make_file("mymain.py", """\
            import colorsys, random
            a = 1
            r, g, b = [random.random() for _ in range(3)]
            hls = colorsys.rgb_to_hls(r, g, b)
            """)

        # Measure without the stdlib, but include colorsys.
        cov1 = coverage.Coverage(cover_pylib=False, include=["*/colorsys.py"])
        self.start_import_stop(cov1, "mymain")

        # some statements were marked executed in colorsys.py
        _, statements, missing, _ = cov1.analysis("colorsys.py")
        self.assertNotEqual(statements, missing)
        # but none were in random.py
        _, statements, missing, _ = cov1.analysis("random.py")
        self.assertEqual(statements, missing) 
开发者ID:nedbat,项目名称:coveragepy-bbmirror,代码行数:20,代码来源:test_api.py

示例4: save

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def save(self, *args, **kwargs):
        if self.color is None:
            values = []
            for r in Resource.objects.filter(color__isnull=False):
                color = [int(r.color[i:i + 2], 16) / 255. for i in range(1, 6, 2)]
                h, _, _ = colorsys.rgb_to_hls(*color)
                values.append(h)
            values.sort()

            if values:
                prv = values[-1] - 1
            opt = 0, 0
            for val in values:
                delta, middle, prv = val - prv, (val + prv) * .5, val
                opt = max(opt, (delta, middle))
            h = opt[1] % 1
            color = colorsys.hls_to_rgb(h, .6, .5)

            self.color = '#' + ''.join(f'{int(c * 255):02x}' for c in color).upper()

        super().save(*args, **kwargs) 
开发者ID:aropan,项目名称:clist,代码行数:23,代码来源:models.py

示例5: lighten_color

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def lighten_color(color, amount=0.5):
    """
    Lightens the given color by multiplying (1-luminosity) by the given amount.
    Input can be matplotlib color string, hex string, or RGB tuple.

    Examples:
    >> lighten_color('g', 0.3)
    >> lighten_color('#F034A3', 0.6)
    >> lighten_color((.3,.55,.1), 0.5)
    """
    import matplotlib.colors as mc
    import colorsys
    try:
        c = mc.cnames[color]
    except:
        c = color
    c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2]) 
开发者ID:flav-io,项目名称:flavio,代码行数:20,代码来源:colors.py

示例6: darken_color

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def darken_color(color, amount=0.5):
    """
    Darkens the given color by multiplying luminosity by the given amount.
    Input can be matplotlib color string, hex string, or RGB tuple.

    Examples:
    >> lighten_color('g', 0.3)
    >> lighten_color('#F034A3', 0.6)
    >> lighten_color((.3,.55,.1), 0.5)
    """
    import matplotlib.colors as mc
    import colorsys
    try:
        c = mc.cnames[color]
    except:
        c = color
    c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], amount * c[1], c[2]) 
开发者ID:flav-io,项目名称:flavio,代码行数:20,代码来源:colors.py

示例7: _change_color_brightness

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def _change_color_brightness(self, color, brightness_factor):
        """
        Depending on the brightness_factor, gives a lighter or darker color i.e. a color with
        less or more saturation than the original color.

        Args:
            color: color of the polygon. Refer to `matplotlib.colors` for a full list of
                formats that are accepted.
            brightness_factor (float): a value in [-1.0, 1.0] range. A lightness factor of
                0 will correspond to no change, a factor in [-1.0, 0) range will result in
                a darker color and a factor in (0, 1.0] range will result in a lighter color.

        Returns:
            modified_color (tuple[double]): a tuple containing the RGB values of the
                modified color. Each value in the tuple is in the [0.0, 1.0] range.
        """
        assert brightness_factor >= -1.0 and brightness_factor <= 1.0
        color = mplc.to_rgb(color)
        polygon_color = colorsys.rgb_to_hls(*mplc.to_rgb(color))
        modified_lightness = polygon_color[1] + (brightness_factor * polygon_color[1])
        modified_lightness = 0.0 if modified_lightness < 0.0 else modified_lightness
        modified_lightness = 1.0 if modified_lightness > 1.0 else modified_lightness
        modified_color = colorsys.hls_to_rgb(polygon_color[0], modified_lightness, polygon_color[2])
        return modified_color 
开发者ID:facebookresearch,项目名称:detectron2,代码行数:26,代码来源:visualizer.py

示例8: make_icon

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def make_icon(template, color):
        """
        Create an icon for the specified user color. It will be used to
        generate on the fly an icon representing the user.
        """
        # Get a light and dark version of the user color
        r, g, b = StatusWidget.ida_to_python(color)
        h, l, s = colorsys.rgb_to_hls(r, g, b)
        r, g, b = colorsys.hls_to_rgb(h, 0.5, 1.0)
        light = StatusWidget.python_to_qt(r, g, b)
        r, g, b = colorsys.hls_to_rgb(h, 0.25, 1.0)
        dark = StatusWidget.python_to_qt(r, g, b)

        # Replace the icon pixel with our two colors
        image = QImage(template)
        for x in range(image.width()):
            for y in range(image.height()):
                c = image.pixel(x, y)
                if (c & 0xFFFFFF) == 0xFFFFFF:
                    image.setPixel(x, y, light)
                if (c & 0xFFFFFF) == 0x000000:
                    image.setPixel(x, y, dark)
        return QPixmap(image) 
开发者ID:IDArlingTeam,项目名称:IDArling,代码行数:25,代码来源:widget.py

示例9: as_hsl_tuple

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def as_hsl_tuple(self, *, alpha: Optional[bool] = None) -> HslColorTuple:
        """
        Color as an HSL or HSLA tuple, e.g. hue, saturation, lightness and optionally alpha; all elements are in
        the range 0 to 1.

        NOTE: this is HSL as used in HTML and most other places, not HLS as used in python's colorsys.

        :param alpha: whether to include the alpha channel, options are
          None - (default) include alpha only if it's set (e.g. not None)
          True - always include alpha,
          False - always omit alpha,
        """
        h, l, s = rgb_to_hls(self._rgba.r, self._rgba.g, self._rgba.b)
        if alpha is None:
            if self._rgba.alpha is None:
                return h, s, l
            else:
                return h, s, l, self._alpha_float()
        if alpha:
            return h, s, l, self._alpha_float()
        else:
            # alpha is False
            return h, s, l 
开发者ID:samuelcolvin,项目名称:pydantic,代码行数:25,代码来源:color.py

示例10: shift

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def shift(color, amount):
  r, g, b, a = color

  print(("rgb", r, g, b, a))

  h, l, s = colorsys.rgb_to_hls(r, g, b)
  print(("hls", h, l, s))
  h *= 360
  h += int(amount)
  h %= 360
  h /= 360.0
  print(("hls", h, l, s))

  r, g, b = colorsys.hls_to_rgb(h, l, s)

  print(("rgb", int(r), int(g), int(b), a))

  return (int(r), int(g), int(b), a) 
开发者ID:ohnorobo,项目名称:pokemon,代码行数:20,代码来源:ChangeHue.py

示例11: lighten

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def lighten(rgb, p):
  h,l,s = colorsys.rgb_to_hls(*(c / 255.0 for c in rgb))
  
  l = p + l - p*l
  return tuple(int(c * 255) for c in colorsys.hls_to_rgb(h,l,s)) 
开发者ID:kevinpt,项目名称:symbolator,代码行数:7,代码来源:sinebow.py

示例12: __apply_luminance

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def __apply_luminance(self, continuous, update_fields):

        r, g, b = self._main_color
        h, l, s = colorsys.rgb_to_hls(r, g, b)
        r, g, b = colorsys.hls_to_rgb(h, self._luminance, s)
        self._command((r, g, b), continuous, update_fields) 
开发者ID:Epihaius,项目名称:panda3dstudio,代码行数:8,代码来源:color_dialog.py

示例13: __set_color

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def __set_color(self, color, continuous=False, update_fields=True, update_gradients=False):

        self._new_color = color
        self._new_color_swatch.set_color(color)
        fields = self._fields

        if fields:

            if continuous:
                if self._clock.real_time > .1:
                    self._clock.reset()
                else:
                    update_fields = False

            if update_fields:

                rgb_scale = 255. if self._rgb_range_id == "255" else 1.
                r, g, b = [c * rgb_scale for c in color]
                fields["red"].set_value(r)
                fields["green"].set_value(g)
                fields["blue"].set_value(b)
                h, l, s = colorsys.rgb_to_hls(*color)
                fields["hue"].set_value(h)
                fields["sat"].set_value(s)
                fields["lum"].set_value(l)

                if update_gradients:
                    lum_ctrl = self._controls["luminance"]
                    lum_ctrl.set_luminance(l)
                    r, g, b = colorsys.hls_to_rgb(h, .5, s)
                    lum_ctrl.set_main_color((r, g, b), continuous=False, update_fields=False)
                    self._controls["hue_sat"].set_hue_sat(h, s) 
开发者ID:Epihaius,项目名称:panda3dstudio,代码行数:34,代码来源:color_dialog.py

示例14: __handle_color_component

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def __handle_color_component(self, component_id, value, state="done"):

        fields = self._fields
        rgb_components = ["red", "green", "blue"]
        hsl_components = ["hue", "sat", "lum"]
        rgb_scale = 255. if self._rgb_range_id == "255" else 1.

        if component_id in rgb_components:

            r, g, b = [fields[c].get_value() / rgb_scale for c in rgb_components]
            h, l, s = colorsys.rgb_to_hls(r, g, b)
            fields["hue"].set_value(h)
            fields["sat"].set_value(s)
            fields["lum"].set_value(l)
            self._controls["luminance"].set_luminance(l)
            r_, g_, b_ = colorsys.hls_to_rgb(h, .5, s)
            self._controls["luminance"].set_main_color((r_, g_, b_), continuous=False)
            self._controls["hue_sat"].set_hue_sat(h, s)

        else:

            h, s, l = [fields[c].get_value() for c in hsl_components]
            r, g, b = colorsys.hls_to_rgb(h, l, s)
            fields["red"].set_value(r * rgb_scale)
            fields["green"].set_value(g * rgb_scale)
            fields["blue"].set_value(b * rgb_scale)

            if component_id == "lum":
                self._controls["luminance"].set_luminance(l)
            else:
                r_, g_, b_ = colorsys.hls_to_rgb(h, .5, s)
                self._controls["luminance"].set_main_color((r_, g_, b_), continuous=False)
                self._controls["hue_sat"].set_hue_sat(h, s)

        self._new_color = color = (r, g, b)
        self._new_color_swatch.set_color(color) 
开发者ID:Epihaius,项目名称:panda3dstudio,代码行数:38,代码来源:color_dialog.py

示例15: RGBToHSL

# 需要导入模块: import colorsys [as 别名]
# 或者: from colorsys import rgb_to_hls [as 别名]
def RGBToHSL(red, green, blue):
    hue, luminosity, saturation = colorsys.rgb_to_hls(
        old_div(float(red), 0xff), old_div(float(green), 0xff), old_div(float(blue), 0xff))

    return hue, saturation, luminosity 
开发者ID:google,项目名称:rekall,代码行数:7,代码来源:colors.py


注:本文中的colorsys.rgb_to_hls方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。