How to display graph colors based on temperature? (Enviro - Python)

This is the display part of the default all-in-one-enviro-mini.py.

This changes the colour of the graph on the screen based on the temperature (from red to green to blue, with gradients), but the default/base colour seems to be red. So if nothing happens and the temperature is stable, the graph seems to always be red.

Would it be possible to make it so that if the temperature is below, say 15°C, the graph is blue, from 15°C to 25°C the graph is green, and only above 25°C the graph is red? (With of course the gradient colours when the temperature nears one of these thresholds.)

# Displays data and text on the 0.96" LCD
def display_text(variable, data, unit):
    # Maintain length of list
    values[variable] = values[variable][1:] + [data]
    # Scale the values for the variable between 0 and 1
    vmin = min(values[variable])
    vmax = max(values[variable])
    colours = [(v - vmin + 1) / (vmax - vmin + 1) for v in values[variable]]
    # Format the variable name and value
    message = "{}: {:.1f} {}".format(variable[:4], data, unit)
    logging.info(message)
    draw.rectangle((0, 0, WIDTH, HEIGHT), (255, 255, 255))
    for i in range(len(colours)):
        # Convert the values to colours from red to blue
        colour = (1.0 - colours[i]) * 0.6
        r, g, b = [int(x * 255.0) for x in colorsys.hsv_to_rgb(colour, 1.0, 1.0)]
        # Draw a 1-pixel wide rectangle of colour
        draw.rectangle((i, top_pos, i + 1, HEIGHT), (r, g, b))
        # Draw a line graph in black
        line_y = HEIGHT - (top_pos + (colours[i] * (HEIGHT - top_pos))) + top_pos
        draw.rectangle((i, line_y, i + 1, line_y + 1), (0, 0, 0))
    # Write the text at the top in black
    draw.text((0, 0), message, font=font, fill=(0, 0, 0))
    st7735.display(img)

This would greatly improve the temperature readability from, say, across the room. The colour would instantly show the range the temperature is in.
The thing is, my Python skills are near to non existent, so any help would be greatly appreciated.

Thanks!

1 Like

Ditto to the above. I’d much rather have it the way you describe. I don’t have the python skills to do it though.
I do believe the way it works now is the highest reading (regardless of what it is) is Red. And then it shifts color to Green then Blue for readings that are lower in value.
I swapped the colors around for Pressure so its Green on the highest reading and Red for lower readings. I changed the r, g, b to g, r, b. I have mine setup with 3 displays though, no having to cycle through them.
g, r, b = [int(x * 255.0) for x in colorsys.hsv_to_rgb(colour, 1.0, 1.0)