def _do_fuzzy(self): from random import seed, randint seed(0) rgb = tuple(randint(0, 255) for i in range(3)) hls = rgb_to_hls(*rgb) hls2rgb = hls_to_rgb(*hls) self.assertEqual(rgb, hls2rgb) rgb2hls = rgb_to_hls(*hls2rgb) self.assertEqual(rgb2hls, hls)
def darken(self, context, amount=None): if amount is None: amount = Value(10.0, '%') hue, lightness, saturation = utils.rgb_to_hls(*self.value) if isinstance(amount, Value): if amount.unit == '%': if not amount.value: return self lightness *= amount.value / 100.0 else: raise errors.EvalException(self.lineno, 'invalid unit %s ' 'for color calculations.' % amount.unit) elif isinstance(amount, Number): lightness -= (amount.value / 100.0) if lightness < 0: lightness = 0.0 return Color(utils.hls_to_rgb(hue, lightness, saturation))
def darken(self, context, amount=None): if amount is None: amount = Value(10.0, '%') hue, lightness, saturation = utils.rgb_to_hls(*self.value) if isinstance(amount, Value): if amount.unit == '%': if not amount.value: return self lightness *= amount.value / 100.0 else: raise errors.EvalException( self.lineno, 'invalid unit %s for color ' 'calculations.' % amount.unit) elif isinstance(amount, Number): lightness -= (amount.value / 100.0) if lightness < 0: lightness = 0.0 return Color(utils.hls_to_rgb(hue, lightness, saturation))
def tint(self, context, lighten=None): """Specifies a relative value by which to lighten the color (e.g. toward white). This works in the opposite manner to the brighten function; a value of 0% produces white (no ink); a value of 50% produces a color halfway between the original and white (e.g. 50% halftone). Less ink also means colour saturation decreases linearly with the amount of ink used. Only positive values between 0-100 for tints are allowed; if you wish to darken an existing color use the darken method or shade_color. N.B. real printing presses -- and therefore some software -- may produce slightly different saturations at different tone curves. If you're really, REALLY anal about the colour that gets reproduced, you should probably trust your design software. For most intents and purposes, though, this is going to be more than sufficient. Valueless tints will be returned unmodified. """ if lighten is None: return self elif isinstance(lighten, (Value, Number)): lighten = lighten.value lighten = abs(lighten) # Positive values only! hue, lit, sat = utils.rgb_to_hls(*self.value) # Calculate relative lightness lavail = 1.0 - lit lused = lavail - (lavail * (lighten / 100)) lnew = lused + (1.0 - lavail) # Corresponding relative (de-)saturation if lit == 0: lit = 1 snew = sat * (1 / (lnew/lit)) return Color(utils.hls_to_rgb(hue, lnew, snew))
def tint(self, context, lighten=None): """Specifies a relative value by which to lighten the color (e.g. toward white). This works in the opposite manner to the brighten function; a value of 0% produces white (no ink); a value of 50% produces a color halfway between the original and white (e.g. 50% halftone). Less ink also means colour saturation decreases linearly with the amount of ink used. Only positive values between 0-100 for tints are allowed; if you wish to darken an existing color use the darken method or shade_color. N.B. real printing presses -- and therefore some software -- may produce slightly different saturations at different tone curves. If you're really, REALLY anal about the colour that gets reproduced, you should probably trust your design software. For most intents and purposes, though, this is going to be more than sufficient. Valueless tints will be returned unmodified. """ if lighten is None: return self elif isinstance(lighten, (Value, Number)): lighten = lighten.value lighten = abs(lighten) # Positive values only! hue, lit, sat = utils.rgb_to_hls(*self.value) # Calculate relative lightness lavail = 1.0 - lit lused = lavail - (lavail * (lighten / 100)) lnew = lused + (1.0 - lavail) # Corresponding relative (de-)saturation if lit == 0: lit = 1 snew = sat * (1 / (lnew / lit)) return Color(utils.hls_to_rgb(hue, lnew, snew))
def hls_to_rgb_overflow(self): self.assertEqual(hls_to_rgb(0.4713, 0.6078, 1.4500), (10, 300, 250))
def hls_to_rgb_underflow(self): self.assertEqual(hls_to_rgb(0.5962, 0.4706, 1.0833), (-10, 100, 250))
def hls_to_rgb(self): self.assertEqual(hls_to_rgb(0.6042, 0.5098, 0.9600), (10, 100, 250))