Python Forum
What kind of object is this?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What kind of object is this?
#1
HI.

Im trying to control a leds color sending data from pc to raspberrypi.
But instead of sending the RGB numbers I want to send a word like PURPLE

If I do this:

PURPLE = (255,0,0)
trellis.color(3,5, PURPLE) # x and y coordinates and color
it works.

But if I send the word PURPLE through serial it doesnt.


is this a string?

PURPLE = (255,0,0)
Reply
#2
Is what a string? Clearly the variable PURPLE refers to a tuple, not a string. The documentation for whatever library this is should tell you what it accepts. In any case, there's probably a library that will provide variables that refer to their RGB values (but I can't recommend any, I'm afraid).
Reply
#3
You could use a dictionary
color_database = {
    "purple1": (155, 48, 255),
    "purple2": (145, 44, 238),
    "purple3": (125, 38, 205),
    "purple4": (85, 26, 139),
}

def rgb(name):
    return color_database[name]

...

treillis.color(3, 5, rgb("purple1"))
On my linux computer, there is the X11 color database at /etc/X11/rgb.txt
Reply
#4
(Dec-26-2020, 11:38 AM)Gribouillis Wrote: You could use a dictionary
color_database = {
    "purple1": (155, 48, 255),
    "purple2": (145, 44, 238),
    "purple3": (125, 38, 205),
    "purple4": (85, 26, 139),
}

def rgb(name):
    return color_database[name]

...

treillis.color(3, 5, rgb("purple1"))
On my linux computer, there is the X11 color database at /etc/X11/rgb.txt



Yes!!!
Thank you
Reply
#5
I would write the RGB code. That way you can send any color and the sender and receiver don't need to maintain color dictionaries. I wrote a Python program that prompts for RGB values and writes a string to a file. I wrote a second Python program there periodically reads the RGB values from the file and sets the background of a tkinter window to that color. I used a file because I don't have a serial port.

The program that displays the color:
from tkinter import *

root = Tk()

def check_for_command():
    try:
        with open('color.txt', 'r') as file:
            for line in file:
                r, g, b = map(int, line.split(','))
                root['bg'] = f'#{r:02x}{g:02x}{b:02x}'
    except:
        pass
    root.after(1000, check_for_command)

check_for_command()

root.mainloop()
The program that writes the color information:
r = int(input('Red '))
g = int(input('Green '))
b = int(input('Blue '))
with open('color.txt', 'w') as file:
    file.write(f'{r},{g},{b}')
After I start the display program I can run the write program multiple times and see the changes in the display window.

I could rewrite this program to pass color names. This is the display side that supports tkinter color names:
from tkinter import *

root = Tk()

def check_for_command():
    try:
        with open('color.txt', 'r') as file:
            for line in file:
                root['bg'] = line
    except:
        pass
    root.after(1000, check_for_command)

check_for_command()

root.mainloop()
This is the program that writes the color file:
with open('color.txt', 'w') as file:
    file.write(input('Enter color name ')
Reply
#6
You can also load the whole X11 colors dictionary in a few lines
def load_x11_colors(filename='/etc/X11/rgb.txt'):
    with open(filename) as ifh:
        next(ifh)
        L = [line.strip.split() for line in ifh]                                         
    return {' '.join(t[3:]): tuple(int(x) for x in t[:3]) for t in L}                    

color_database = load_x11_colors()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What kind of list is this? jesse68 2 1,103 Jun-29-2022, 05:02 PM
Last Post: rob101
  splitting lines, need to add element of same kind tester_V 6 3,069 Feb-20-2021, 11:51 PM
Last Post: tester_V
  How to do this kind of IF STATEMENT? glennford49 4 2,576 Jun-17-2020, 09:45 AM
Last Post: glennford49
  How to write a code to get this kind of output in IDLE kavindu 5 3,496 Oct-28-2018, 07:46 PM
Last Post: wavic
  Newbie question for a kind of rolling average of list of lits zydjohn 3 3,287 Dec-16-2017, 11:41 PM
Last Post: ezdev
  Compiler fault or some kind of weird referencing bug? Joseph_f2 11 9,064 May-09-2017, 08:50 PM
Last Post: volcano63
  want to know the kind of object whether its a python or json object johnkennykumar 5 53,350 Jan-21-2017, 08:47 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020