當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。