jonasjacek/colors

Reworked the Table

axgkl opened this issue · 2 comments

axgkl commented

Hi, thanks for the table.

As you say there are clashes and so I reworked it.

Sorted / renamed those often conflicting names using your luminance infos into descending order regarding brightness.

image

Btw: How did you calculate those hsl values, sometimes they look a bit off, visually...

Anyway, source for the fix:

colors = <paste your json table here>

if __name__ == "__main__":

    # first remove all numbers from the names, they don't fit:
    names = {}
    for m in colors:
        n = m["name"]
        while n[-1].isdigit():
            n = n[:-1]
        m["name"] = n
        names.setdefault(n, []).append(m)
    # we nave now in names:
    # {'Red': [{'name': 'Red', id: 9, ..., hsl: }, {'name': 'Red', id:196, ...}..

    # first Red is not red:
    red = names["Red"].pop(0)
    red["name"] = "Salmon"
    names["Salmon"].append(red)

    # now sort the lists per luminance and set the numbers from high to low lum.:
    nn = {}
    for n, ms in names.items():
        if len(ms) > 1:
            ms = sorted(
                ms,
                key=lambda k: 10000
                - float("%s.%s" % (k["hsl"]["l"], int(k["hsl"]["h"]))),
            )
        # first greys are actually darker whites, so:
        if n == "Grey":
            ws, ms = ms[:3], ms[3:]
            i = 1
            for w in ws:
                wn = "White%s" % i
                nn[wn] = w
                i += 1
        i = ""
        for m in ms:
            # DarkOlive makes no sense, superbright first - and there is no Olive:
            na = (m["name"] + str(i)).replace("DarkOlive", "Olive")
            m["name"] = na
            i = 1 if not i else i + 1
            nn[na] = m

    names = nn
    table = {}
    for n, m in names.items():
        table[m["colorId"]] = m

    import sys, json

    sys.argv.append("")
    if sys.argv[1] == "table":
        print(json.dumps(table, sort_keys=1))
    elif sys.argv[1] == "names":
        print(json.dumps(names, sort_keys=1))
    else:
        tm = "%(colorId)4s \x1b[38;5;%(colorId)sm%(name)s\x1b[0m"
        for n, m in names.items():
            print(tm % m)

Then save this as foo.py and run python3 foo.py [table|names]

If you say python3 foo.py names | jq . you get

image

@axgkl - That looks extremely promising. I will start experimenting with your script early November. I have just reserved a small time slot. If you want, you can also send a pull request and I just merge your changes.

To answer your question "How did you calculate those hsl values, sometimes they look a bit off, visually...": They have been merged in as well. See here: #2

Thank you for taking the time and your efforts to help fix errors in this repository. I highly appreciate it!

Hi @axgkl ,
I have now investigated your suggestion.

I will not apply your suggestions, as they do not solve the problem. Just changing names on duplicate names, does not solve the underlying issue. The suggested sort order sounded good at first but you are right, visually the sorting seems off sometimes. I think this should be fixed first, otherwise it will just introduce new errors.

However, I want to thank you for taking the time trying to help to fix errors in this repository. I really appreciate this.