clamp hsla values

This commit is contained in:
frank 2021-05-27 16:45:57 -04:00
parent 1e85c7b619
commit 2a97cd761e
2 changed files with 27 additions and 8 deletions

View File

@ -87,8 +87,7 @@ class Sprite(Animation):
self.get_current_frameset().set_framerate(framerate)
self.register_interval()
def load_from_path(self, path, transparency=False, ppa=True, key=None,
query=None, omit=False):
def load_from_path(self, path, transparency=False, ppa=True, key=None, query=None, omit=False):
for frame in load_frames(self.get_resource(path), transparency, ppa, key, query):
self.add_frame(frame, omit)

View File

@ -90,8 +90,8 @@ def get_point_on_circle(center, radius, angle, translate_angle=True):
def get_range_steps(start, end, count, omit=[]):
'''
Iterator that yields `count` number of steps from `start` to `end` as floats. Indicies in the
omit parameter will be skipped
Iterator that yields evenly spaced steps from `start` to `end` as floats based on step `count`. Indicies in the
omit parameter will be skipped.
'''
# normalize array indicies (for example -1 becomes count - 1)
for ii in range(len(omit)):
@ -298,16 +298,25 @@ def get_wrapped_text_surface(font, text, width, antialias=True, color=(0, 0, 0),
return get_boxed_surface(surface, background, border, border_width, padding)
def replace_color(surface, color, replacement):
'''
Replace a color on a surface without creating a copy
'''
pixels = PixelArray(surface)
pixels.replace(color, replacement)
del pixels
def get_color_swapped_surface(surface, color, replacement):
'''
Get a copy of given Surface with given color replaced by given replacement
'''
swapped = surface.copy()
replace_color(swapped, color, replacement)
return swapped
def get_busy_channel_count():
'''
Return count of audio channels currently playing audio
'''
count = 0
for index in range(get_num_channels()):
count += Channel(index).get_busy()
@ -434,24 +443,35 @@ def get_blinds_frames(surface, step=.05, count=4, fill=(0, 0, 0, 0)):
return frames
def get_hsla_color(hue, saturation=100, lightness=50, alpha=100):
'''
Get a pygame Color object from HSLA value
'''
color = Color(0, 0, 0, 0)
color.hsla = hue % 360, saturation, lightness, alpha
color.hsla = hue % 360, clamp(saturation, 0, 100), clamp(lightness, 0, 100), clamp(alpha, 0, 100)
return color
def get_random_hsla_color(hue_range=(0, 359), saturation_range=(0, 100),
lightness_range=(0, 100), alpha_range=(100, 100)):
def get_random_hsla_color(hue_range=(0, 359), saturation_range=(0, 100), lightness_range=(0, 100), alpha_range=(100, 100)):
'''
Get a random color with constrained hue, saturation, lightness and alpha
'''
return get_hsla_color(
random.randint(*hue_range), random.randint(*saturation_range), random.randint(*lightness_range),
random.randint(*alpha_range))
def get_hsva_color(hue, saturation=100, value=100, alpha=100):
'''
Get a pygame Color object from HSVA value
'''
color = Color(0, 0, 0, 0)
color.hsva = hue % 360, saturation, value, alpha
return color
def get_lightened_color(color, lightness):
'''
Return a copy of the provided color with specified lightness
'''
h, s, _, a = color.hsla
return get_hsla_color(h, s, lightness, a)
return get_hsla_color(h, s, clamp(lightness, 0, 100), a)
def get_glow_frames(radius, segments, colors=[(0, 0, 0), (255, 255, 255)], minsize=4, transparency=True):
frames = []