theOehrly / Fast-F1

FastF1 is a python package for accessing and analyzing Formula 1 results, schedules, timing data and telemetry
https://docs.fastf1.dev
MIT License
2.43k stars 256 forks source link

[ENH] Drivers colors #480

Closed formulatimer closed 1 month ago

formulatimer commented 10 months ago

Proposed new feature or change:

Hi,

When I run the example plot_position_changes and change the year to 2022. It fails due to the color = fastf1.plotting.driver_color(abb) being set up only for the current year, and there are no colors for VET, MSC,...

I have solved the issue with:

try:
              color=ff1.plotting.driver_color(abb)
except:
              color=ff1.plotting.team_color(term.get_driver(abb).TeamName)

However, I am encountering an issue where ALO, VET, and STR have Aston Martin colors, and ALO should have Alpine colors.

Do you consider improving the driver color dictionary? Maybe the data can be imported from an API to obtain the exact color for each race.

Same issues could happen in the future if a similar thing occurs to:

Casper-Guo commented 10 months ago

Discussion is at #218

theOehrly commented 10 months ago

Yes, this is a long-standing issue. But not that easy to solve, as you notice, when reading the other discussion. I hope to get this fixed for next season. But there won't be any noticeable progress before that.

formulatimer commented 9 months ago

Hi, I have read the discussion and tried to generate different colors by changing the lightness, but it was not successful. I am thinking that we can use the TeamColor in the DriverList for the teams.

I have found a list of 20 simple colors that enables easy contrasting. We can assign a color for each driver, however the driver's color will vary in every session, and it might seem strange to see HAM in red and VER in orange...

It solves the year-color problems, prevents similar colors, and eliminates the need to add colors manually when a new driver joins a session

Let me know your thoughts about it

Casper-Guo commented 9 months ago

I feel that defeats the purpose of providing driver colors. They are given in the plotting module for consistency. Also in this case each driver is strongly associated with their team color, I doubt many user will use the new color mapping if that association is not guarenteed

theOehrly commented 9 months ago

@manpean I agree with @Casper-Guo. That's not a solution most people are interested in.

formulatimer commented 9 months ago

Hi again,

I have been testing a new code.

The team's colors are converted from hex to CIELab. For each team color, the code searches for a color that fulfills three conditions:

The Delta_E between the color and other team colors must be higher than 'diff'. The Delta_E between the color and the same team color must be lower than 'similar_s'. The Delta_E between the color and the same team color must be higher than 'similar_i'. The colors are generated by iterating through RGB values. However, this code has two problems:

It is not efficient; it has three nested 'for' loops, and each loop iterates 256 times, resulting in 16,777,216 possible colors. As I have simplified the iterations to make it faster, if the conditions are too restrictive, the code may not find a solution. Attached is a plot generated using this code where: diff=4; similar_s=6; similar_i=3

Colors

I attach the code, maybe you can find a better way to do it.

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import colorspacious

from fastf1 import plotting

plt.rcParams.update(plt.rcParamsDefault)
plotting.setup_mpl()

TEAM_COLORS = ["#358C75", "#F91536", "#F58020", "#6CD3BF", "#5E8FAA", "#3671C6", 
    "#B6BABD", "#2293D1", "#37BEDD", "#C92D4B"]

TEAMS=["Aston Martin", "Ferrari", "Mclaren", "Mercedes", "Alpha Tauri", "Red Bull",
       "Haas", "Alpine", "Williams", "Alfa Romeo"]

diff=4              #Minimun difference color with other team colors
similar_s=6         #Maximun difference between the team colors
similar_i=3         #Minimun difference between the team colors

Lab_colors = list()
Results = list()

for color in TEAM_COLORS:
    color_rgb = colors.hex2color(color)
    color_lab = colorspacious.cspace_convert(color_rgb, "sRGB1", "CAM02-UCS")

    Lab_colors.append(color_lab)

for i in range(len(Lab_colors)):
    stop_loops=False
    for r in range(32):
        # print(f"r: {r}")
        for g in range(32):
            for b in range(32):
                color_rgb = colorspacious.cspace_convert(Lab_colors[i], "CAM02-UCS", "sRGB1")
                color_lab = colorspacious.cspace_convert(((color_rgb[0]+r*8/256)%1, (color_rgb[1]+g*8/256)%1, (color_rgb[2]+b*8/256)%1), "sRGB1", "CAM02-UCS")

                if all(colorspacious.deltaE(color_lab, otro_color) > diff and colorspacious.deltaE(color_lab, Lab_colors[i]) < similar_s and colorspacious.deltaE(color_lab, Lab_colors[i]) > similar_i for index, otro_color in enumerate(Lab_colors) if index != i):
                    color_rgb = colorspacious.cspace_convert(color_lab, "CAM02-UCS", "sRGB1")
                    color_hex = colors.to_hex(color_rgb)
                    TEAM_COLORS.append(color_hex)
                    Lab_colors.append(color_lab)
                    Results.append([TEAM_COLORS[i], color_hex])
                    stop_loops=True
                    print(f"Color {i+1} found: ({r/256},{g/256},{b/256})\nDiff:{colorspacious.deltaE(color_lab, Lab_colors[i])}\n")
                    break

                if stop_loops:
                    break  
            if stop_loops:
                break  
        if stop_loops:
            break

fig = plt.figure()
ax = fig.add_subplot()

for i, pair in enumerate(Results):
    for j, color in enumerate(pair):
        ax.plot([0, 1], [i+j*0.15, i+j*0.15], color=color, linewidth=4, markerfacecolor='black', markeredgecolor='black', marker='o')

ax.set_yticks(range(0, len(Results)))
ax.set_yticklabels(TEAMS)

# plt.savefig("Colors", dpi=300)
plt.show()

# print(Results) 
formulatimer commented 9 months ago

@Casper-Guo I have read an article that explains how the difference between colors are measured using Delta E

The code I attached uses Delta E 1976, it means that Delta E is the lenght of the vector that starts in color 1 and ends in color 2 in a 3D space.

theOehrly commented 1 month ago

As of v3.4.0 and #585 driver specific colours are now deprecated and the whole fastf1.plotting module has been reimplemented with new API.