def test6_pyglet(bit): """ Abandoned!!! Not dynamic yet!!! Same test as test6, but using pyglet directly instead of Psychopy """ import pyglet from pyglet.gl import gl from psychopy.hardware import keyboard import time kb = keyboard.Keyboard() # colors def changegun(c, d): new = c.astype(float) new[0] = new[0] + d # R new[1] = new[1] - d # G new[2] = new[2] - d # B return new Crgb = ColorPicker().Crgb color = Crgb.astype(int) newcolor1 = changegun(color, 1) newcolor2 = changegun(color, -1) color1_1 = [color, color, color, newcolor1, newcolor1, newcolor1] # upper in condition 1 color1_2 = [color, color, color, newcolor2, newcolor2, newcolor2] # upper in condition 2 color2_1 = [ changegun(color, 0.), changegun(color, 0.2), changegun(color, 0.4), changegun(color, 0.6), changegun(color, 0.8), changegun(color, 1.0) ] color2_2 = [ changegun(color, 0.), changegun(color, -0.2), changegun(color, -0.4), changegun(color, -0.6), changegun(color, -0.8), changegun(color, -1.0) ] # convert to [0, 1] Crgb = Crgb / (2**8 - 1) rgb1_1 = [x / (2**8 - 1) for x in color1_1] # upper in condition 1 rgb1_2 = [x / (2**8 - 1) for x in color1_2] # upper in condition 2 rgb2_1 = [x / (2**8 - 1) for x in color2_1] rgb2_2 = [x / (2**8 - 1) for x in color2_2] background = (Crgb[0], Crgb[1], Crgb[2], 1) # RGB and alpha value # set up context and window # platform = pyglet.window.get_platform() # display = platform.get_default_display() # screen = display.get_default_screen() # template = pyglet.gl.Config(red_size=bit, green_size=bit, blue_size=bit) # config = screen.get_best_config(template) # context = config.create_context(None) # config = pyglet.gl.Config(red_size=bit, green_size=bit, blue_size=bit) config = pyglet.gl.Config(buffer_size=bit * 3) winsize = 1900 window = pyglet.window.Window(caption='OpenGL', resizable=True, config=config, fullscreen=True) frameN = 0 while True: if frameN % 2: rgb1 = rgb1_1 rgb2 = rgb2_1 color1 = color1_1 color2 = color2_1 else: rgb1 = rgb1_2 rgb2 = rgb2_2 color1 = color1_2 color2 = color2_2 @window.event def on_draw(): # clears the background with the background color gl.glClearColor(*background) gl.glClear(gl.GL_COLOR_BUFFER_BIT) # draw in a loop boundary = winsize - 100 gap = (winsize - boundary) / 2 i_width = boundary / len(rgb1) i_height = boundary / 2 # the first line for idx, rgb in enumerate(rgb1): gl.glColor3f(*rgb) gl.glBegin( gl.GL_QUADS ) # start drawing a rectangle in counter-clockwise (CCW) order gl.glVertex2f(gap + idx * i_width, gap + i_height) # bottom left point gl.glVertex2f(gap + (idx + 1) * i_width, gap + i_height) # bottom right point gl.glVertex2f(gap + (idx + 1) * i_width, gap + boundary) # top right point gl.glVertex2f(gap + idx * i_width, gap + boundary) # top left point gl.glEnd() label = pyglet.text.Label(str(color1[idx]), font_size=7, x=gap + idx * i_width, y=gap + boundary + 20) label.draw() # # # the second line for idx, rgb in enumerate(rgb2): gl.glColor3f(*rgb) gl.glBegin( gl.GL_QUADS ) # start drawing a rectangle in counter-clockwise (CCW) order gl.glVertex2f(gap + idx * i_width, gap) # bottom left point gl.glVertex2f(gap + (idx + 1) * i_width, gap) # bottom right point gl.glVertex2f(gap + (idx + 1) * i_width, gap + i_height) # top right point gl.glVertex2f(gap + idx * i_width, gap + i_height) # top left point gl.glEnd() label = pyglet.text.Label(str(color2[idx]), font_size=7, x=gap + idx * i_width, y=gap - 20) label.draw() frameN += 1 pyglet.app.run() kb.clock.reset() if kb.getKeys(): # press any key to quit core.quit() else: time.sleep(1) # change every 1 sec
def test6(bit, diff=1): """ Dynamic version of test5. :param bit: 8 or 10 :param diff: difference in RGB255 scale :return: """ from psychopy.hardware import keyboard import time # mon = monitors.Monitor(name='VIEWPixx LITE', width=38, distance=57) # color = ColorPicker().Crgb color = color.astype(int) def changegun(c, d): new = c.astype(float) new[0] = new[0] + d # R new[1] = new[1] - d # G new[2] = new[2] - d # B return new newcolor1 = changegun(color, diff) newcolor2 = changegun(color, -diff) color1_1 = [color, color, color, newcolor1, newcolor1, newcolor1] # upper in condition 1 color1_2 = [color, color, color, newcolor2, newcolor2, newcolor2] # upper in condition 2 color2_1 = [ changegun(color, 0.), changegun(color, 0.2), changegun(color, 0.4), changegun(color, 0.6), changegun(color, 0.8), changegun(color, 1.0) ] color2_2 = [ changegun(color, 0.), changegun(color, -0.2), changegun(color, -0.4), changegun(color, -0.6), changegun(color, -0.8), changegun(color, -1.0) ] # convert to [-1, 1] Crgb = ColorPicker().Crgb / (2**8 - 1) * 2 - 1 rgb1_1 = [x / (2**8 - 1) * 2 - 1 for x in color1_1] # upper in condition 1 rgb1_2 = [x / (2**8 - 1) * 2 - 1 for x in color1_2] # upper in condition 2 rgb2_1 = [x / (2**8 - 1) * 2 - 1 for x in color2_1] rgb2_2 = [x / (2**8 - 1) * 2 - 1 for x in color2_2] win = visual.Window(fullscr=True, color=Crgb, colorSpace='rgb', bpc=(bit, bit, bit), depthBits=bit, units='norm') kb = keyboard.Keyboard() boundary = 0.8 num = len(color1_1) colorbar1 = visual.ElementArrayStim(win, units='norm', nElements=num, elementMask=None, elementTex=None, sizes=(boundary * 2 / num, boundary / 2), colorSpace='rgb') colorbar2 = visual.ElementArrayStim(win, units='norm', nElements=num, elementMask=None, elementTex=None, sizes=(boundary * 2 / num, boundary / 2), colorSpace='rgb') colorbar1.xys = [ (x, boundary / 4) for x in np.linspace(-boundary, boundary, num, endpoint=False) + boundary / num ] colorbar2.xys = [ (x, -boundary / 4) for x in np.linspace(-boundary, boundary, num, endpoint=False) + boundary / num ] frameN = 0 while True: if frameN % 2: rgb1 = rgb1_1 rgb2 = rgb2_1 color1 = color1_1 color2 = color2_1 else: rgb1 = rgb1_2 rgb2 = rgb2_2 color1 = color1_2 color2 = color2_2 colorbar1.colors = rgb1 colorbar2.colors = rgb2 colorbar1.draw() colorbar2.draw() for idx, x in enumerate( np.linspace(-boundary, boundary, num, endpoint=False) + boundary / num): text1 = visual.TextStim( win, text=str(color1[idx]), pos=(x, 0.7), height=0.028) # position set in weird way but works text2 = visual.TextStim(win, text=str(color2[idx]), pos=(x, -0.7), height=0.028) text1.draw() text2.draw() win.flip() frameN += 1 kb.clock.reset() if kb.getKeys(): # press any key to quit core.quit() else: time.sleep(1) # change every 1 sec
def test5_pyglet(bit): """ Same test as test5, but using pyglet directly instead of Psychopy TODO: adjust the size on VIEWPixx """ import pyglet from pyglet.gl import gl # colors def changegun(c, d): new = c.astype(float) new[0] = new[0] + d # R new[1] = new[1] - d # G new[2] = new[2] - d # B return new Crgb = ColorPicker().Crgb color = Crgb.astype(int) color1 = [ color, color, color, changegun(color, 1.), changegun(color, 1.), changegun(color, 1.) ] color2 = [ color, changegun(color, .2), changegun(color, .4), changegun(color, .6), changegun(color, .8), changegun(color, 1.) ] # convert to [0, 1] Crgb = Crgb / (2**8 - 1) rgb1 = [x / (2**8 - 1) for x in color1] rgb2 = [x / (2**8 - 1) for x in color2] background = (Crgb[0], Crgb[1], Crgb[2], 1) # RGB and alpha value # set up context and window # config = pyglet.gl.Config(red_size=bit, gre""" rewrite the test5 with pyglet"""en_size=bit, blue_size=bit) config = pyglet.gl.Config(buffer_size=bit * 4) winsize = 1200 window = pyglet.window.Window(caption='OpenGL', resizable=True, config=config, fullscreen=True) @window.event def on_draw(): # clears the background with the background color gl.glClearColor(*background) gl.glClear(gl.GL_COLOR_BUFFER_BIT) # draw in a loop boundary = winsize - 200 gap = (winsize - boundary) / 2 i_width = boundary / len(rgb1) i_height = boundary / 2 # the first line for idx, rgb in enumerate(rgb1): gl.glColor3f(*rgb) gl.glBegin( gl.GL_QUADS ) # start drawing a rectangle in counter-clockwise (CCW) order gl.glVertex2f(gap + idx * i_width, gap + i_height) # bottom left point gl.glVertex2f(gap + (idx + 1) * i_width, gap + i_height) # bottom right point gl.glVertex2f(gap + (idx + 1) * i_width, gap + boundary) # top right point gl.glVertex2f(gap + idx * i_width, gap + boundary) # top left point gl.glEnd() label = pyglet.text.Label(str(color1[idx]), font_size=10, x=gap + idx * i_width, y=gap + boundary + 20) label.draw() # # # the second line for idx, rgb in enumerate(rgb2): gl.glColor3f(*rgb) gl.glBegin( gl.GL_QUADS ) # start drawing a rectangle in counter-clockwise (CCW) order gl.glVertex2f(gap + idx * i_width, gap) # bottom left point gl.glVertex2f(gap + (idx + 1) * i_width, gap) # bottom right point gl.glVertex2f(gap + (idx + 1) * i_width, gap + i_height) # top right point gl.glVertex2f(gap + idx * i_width, gap + i_height) # top left point gl.glEnd() label = pyglet.text.Label(str(color2[idx]), font_size=10, x=gap + idx * i_width, y=gap - 20) label.draw() pyglet.app.run()
def test5(bit, diff=1): """ Show static 6 x 2 colored patches: - 1st row with 1-bit step in RGB255 - 2nd row with 0.2-bit step -- should be distinguishable on 10-bit :param bit: 8 or 10 :param diff: difference in RGB255 scale :return: """ color = ColorPicker().Crgb color = color.astype(int) def changegun(c, d): new = c.astype(float) new[0] = new[0] + d # R new[1] = new[1] - d # G new[2] = new[2] - d # B return new newcolor = changegun(color, diff) color1 = [color, color, color, newcolor, newcolor, newcolor] # upper row color2 = [ changegun(color, 0.), changegun(color, 0.2), changegun(color, 0.4), changegun(color, 0.6), changegun(color, 0.8), changegun(color, 1.0) ] # lower row # convert to [-1, 1] Crgb = ColorPicker().Crgb / (2**8 - 1) * 2 - 1 rgb1 = [x / (2**8 - 1) * 2 - 1 for x in color1] rgb2 = [x / (2**8 - 1) * 2 - 1 for x in color2] win = visual.Window(fullscr=True, color=Crgb, colorSpace='rgb', bpc=(bit, bit, bit), depthBits=bit) boundary = 0.8 num = len(color1) colorbar1 = visual.ElementArrayStim(win, units='norm', nElements=num, elementMask=None, elementTex=None, sizes=(boundary * 2 / num, boundary / 2), colorSpace='rgb') colorbar2 = visual.ElementArrayStim(win, units='norm', nElements=num, elementMask=None, elementTex=None, sizes=(boundary * 2 / num, boundary / 2), colorSpace='rgb') colorbar1.xys = [ (x, boundary / 4) for x in np.linspace(-boundary, boundary, num, endpoint=False) + boundary / num ] colorbar2.xys = [ (x, -boundary / 4) for x in np.linspace(-boundary, boundary, num, endpoint=False) + boundary / num ] colorbar1.colors = rgb1 colorbar2.colors = rgb2 print(colorbar2.colors) colorbar1.draw() colorbar2.draw() for idx, x in enumerate( np.linspace(-boundary, boundary, num, endpoint=False) + boundary / num): text1 = visual.TextStim( win, text=str(color1[idx]), pos=(x, 0.7), height=0.028) # position set in weird way but works text2 = visual.TextStim(win, text=str(color2[idx]), pos=(x, -0.7), height=0.028) text1.draw() text2.draw() win.flip() if event.waitKeys(): win.close() return