|
| 1 | +# This file is part of visiomode. |
| 2 | +# Copyright (c) 2021 Constantinos Eleftheriou <Constantinos.Eleftheriou@ed.ac.uk> |
| 3 | +# Distributed under the terms of the MIT Licence. |
| 4 | + |
| 5 | +import random |
| 6 | +import colorsys |
| 7 | + |
| 8 | +import pygame as pg |
| 9 | +import colorsys |
| 10 | + |
| 11 | + |
| 12 | +import visiomode.stimuli.solid_colour as solidcolour |
| 13 | + |
| 14 | +from visiomode import stimuli |
| 15 | + |
| 16 | + |
| 17 | +class VariableColourFixedRange(solidcolour.SolidColour): |
| 18 | + form_path = "stimuli/variable_colour_fixed.html" |
| 19 | + |
| 20 | + def __init__(self, background, base_colour, saturations, luminances, **kwargs): |
| 21 | + super().__init__(background, **kwargs) |
| 22 | + |
| 23 | + self.colour = base_colour |
| 24 | + self.rgb = pg.Color(base_colour) |
| 25 | + |
| 26 | + self.image = pg.Surface((self.width, self.height)) |
| 27 | + self.image.fill(self.rgb) |
| 28 | + self.rect = self.image.get_rect() |
| 29 | + self.area = self.screen.get_rect() |
| 30 | + |
| 31 | + def get_details(self): |
| 32 | + """Returns a dictionary of stimulus attributes.""" |
| 33 | + return { |
| 34 | + "id": self.get_identifier(), |
| 35 | + "common_name": self.get_common_name(), |
| 36 | + "width": self.width, |
| 37 | + "height": self.height, |
| 38 | + "center_x": self.rect.centerx, |
| 39 | + "center_y": self.rect.centery, |
| 40 | + "colour_hex": self.colour, |
| 41 | + "colour_rgba": str(self.rgb), |
| 42 | + "hue": self.rgb.hsla[0], |
| 43 | + "saturation": self.rgb.hsla[1], |
| 44 | + "luminance": self.rgb.hsla[2], |
| 45 | + "alpha": self.rgb.hsla[3], |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +class VariableColourUniformRange(stimuli.Stimulus): |
| 50 | + form_path = "stimuli/variable_colour_uniform.html" |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + background, |
| 55 | + hue: float, |
| 56 | + saturation_min: float, |
| 57 | + saturation_max: float, |
| 58 | + luminance_min: float, |
| 59 | + luminance_max: float, |
| 60 | + **kwargs, |
| 61 | + ): |
| 62 | + super().__init__(background, **kwargs) |
| 63 | + |
| 64 | + self.hue = float(hue) |
| 65 | + if self.hue < 0: |
| 66 | + self.hue = 360 - abs(self.hue) |
| 67 | + self.saturation_min = float(saturation_min) |
| 68 | + self.saturation_max = float(saturation_max) |
| 69 | + self.luminance_min = float(luminance_min) |
| 70 | + self.luminance_max = float(luminance_max) |
| 71 | + |
| 72 | + self.colour = pg.Color(0, 0, 0) |
| 73 | + self.colour.hsla = (self.hue, self.saturation_min, self.luminance_min, 100) |
| 74 | + |
| 75 | + self.image = pg.Surface((self.width, self.height)) |
| 76 | + # self.image.fill(self.colour) |
| 77 | + self.rect = self.image.get_rect() |
| 78 | + self.area = self.screen.get_rect() |
| 79 | + |
| 80 | + def generate_new_trial(self): |
| 81 | + super().generate_new_trial() |
| 82 | + self.trial_luminance = random.uniform(self.luminance_min, self.luminance_max) # noqa: S311 |
| 83 | + self.trial_saturation = random.uniform(self.saturation_min, self.saturation_max) # noqa: S311 |
| 84 | + |
| 85 | + self.colour.hsla = (self.hue, self.trial_saturation, self.trial_luminance, 100) |
| 86 | + self.image.fill(self.colour) |
| 87 | + |
| 88 | + def get_details(self): |
| 89 | + """Returns a dictionary of stimulus attributes.""" |
| 90 | + return { |
| 91 | + "id": self.get_identifier(), |
| 92 | + "common_name": self.get_common_name(), |
| 93 | + "width": self.width, |
| 94 | + "height": self.height, |
| 95 | + "center_x": self.rect.centerx, |
| 96 | + "center_y": self.rect.centery, |
| 97 | + "colour_rgba": str(self.colour), |
| 98 | + "hue": self.hue, |
| 99 | + "saturation": self.colour.hsla[1], |
| 100 | + "luminance": self.colour.hsla[2], |
| 101 | + "alpha": self.colour.hsla[3], |
| 102 | + } |
0 commit comments