Skip to content

Commit 026ed22

Browse files
committed
add colour discrimination stimulus with uniform sampling
1 parent 9f19b39 commit 026ed22

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<div class="row">
2+
<div class="col">
3+
<label for="{{ idx or ''}}hue">Hue (degrees)</label>
4+
<input id="{{ idx or ''}}hue" class="form-control mb-3" type="number" value="30" min="-360" max=360 step="1">
5+
</div>
6+
<div class="col">
7+
<label></label>
8+
<div class="card mt-1" id="{{ idx or ''}}hue_viewer">
9+
<div class="card-body"></div>
10+
</div>
11+
</div>
12+
</div>
13+
14+
<div class="row">
15+
<div class="col">
16+
<label for="{{ idx or ''}}saturation_min">Saturation min (0-100%)</label>
17+
<input id="{{ idx or ''}}saturation_min" class="form-control mb-3" type="number" value="50" min="0" max="100"
18+
step="0.1">
19+
</div>
20+
<div class="col">
21+
<label for="{{ idx or ''}}saturation_max">Saturation max (0-100%)</label>
22+
<input id="{{ idx or ''}}saturation_max" class="form-control mb-3" type="number" value="50" min="0" max="100"
23+
step="0.1">
24+
</div>
25+
</div>
26+
27+
<div class="row">
28+
<div class="col">
29+
<label for="{{ idx or ''}}luminance_min">Luminance min (0-100%)</label>
30+
<input id="{{ idx or ''}}luminance_min" class="form-control mb-3" type="number" value="50" min="0" max="100"
31+
step="0.1">
32+
</div>
33+
<div class="col">
34+
<label for="{{ idx or ''}}luminance_max">Luminance max (0-100%)</label>
35+
<input id="{{ idx or ''}}luminance_max" class="form-control mb-3" type="number" value="50" min="0" max="100"
36+
step="0.1">
37+
</div>
38+
</div>
39+
40+
41+
<script>
42+
hue_selector = document.getElementById("{{ idx or ''}}hue");
43+
hue_viewer = document.getElementById("{{ idx or ''}}hue_viewer");
44+
45+
hue_selector.oninput = function () {
46+
hue_viewer.style.backgroundColor = `hsl(${hue_selector.value} 50 50)`;
47+
}
48+
hue_selector.oninput();
49+
</script>

0 commit comments

Comments
 (0)