drep icon indicating copy to clipboard operation
drep copied to clipboard

Contrast of text in dendograms

Open danpal96 opened this issue 1 year ago • 1 comments

Version (Anaconda): drep 3.5.0 pyhdfd78af_0 bioconda

First of all, thanks for your work, dRep has been very useful in my work.

One thing that could be improved is the use of colors in the text of the dendrograms, some of them have very low contrast against white. According to https://webaim.org/resources/contrastchecker/ is recommendable that the text have at least a 4.5 contrast ratio with the background. Here is an image that shows the contrast ratio of the used colors (I think that you are using the jet colormap):

image

This could be solved by choosing other colors for the text but I think that a simpler solution is to use a circle filled with the color next to text, something like this: Primary_clustering_dendrogram_circles For comparison: Primary_clustering_dendrogram

Code used for the contrast image:

import matplotlib.pyplot as plt
import matplotlib.colors as mc
import numpy as np
import httpx
import asyncio
from dataclasses import dataclass

@dataclass(slots=True)
class Colors:
    fg: str
    bg: str
    contrast: float | None = None

async def add_contrast(session, colors):
    try:
        url = f"https://webaim.org/resources/contrastchecker/?fcolor={mc.rgb2hex(colors.fg)[1:]}&bcolor={mc.rgb2hex(colors.bg)[1:]}&api"
        response = await session.get(url)
        response.raise_for_status()
        colors.contrast = float(response.json()["ratio"])
    except httpx.HTTPStatusError as e:
        print(f"Error in the request: {e.response.status_code} for URL {url}")

async def add_contrasts(colors_list):
    async with httpx.AsyncClient() as session:
        tasks = []
        for colors in colors_list:
            task = asyncio.create_task(add_contrast(session, colors))
            tasks.append(task)
        
        await asyncio.gather(*tasks)

async def get_colors_list(colormap):
    vals = np.linspace(0,1,20)
    #np.random.shuffle(vals)
    white = (1, 1, 1)
    colors_list = list(map(lambda c: Colors(c, white), colormap(vals)))
    await add_contrasts(colors_list)
    return colors_list

def plot_contrasts(colors_list):
    fig, ax = plt.subplots(figsize=(4, 4))
    
    ax.axis('off')
    
    length = len(colors_list)
    for i, colors in enumerate(colors_list):
        plt.figtext(0, i / length, colors.contrast, color="black")
        plt.figtext(0.1, i / length, "The quick brown fox jumps over the lazy dog", color=colors.fg)

    plt.show()

colors_list_jet = await get_colors_list(plt.cm.jet)
plot_contrasts(colors_list_jet)

danpal96 avatar Jun 28 '24 15:06 danpal96