jtdaugherty / vty

A high-level ncurses alternative written in Haskell
BSD 3-Clause "New" or "Revised" License
319 stars 57 forks source link

rewrite the color logic #209

Closed KommuSoft closed 3 years ago

jtdaugherty commented 3 years ago

Can you provide a bit more information about this change? Such as:

KommuSoft commented 3 years ago

This is mainly for performance purposes. Right now it can take up to 241 guards, and each guard requires three boolean checks.

In the new approach after 25 guards (75 boolean checks), we work with a helper function that will for each color map it to a [0..5] range, and then we perform calculations to obtain the number. This helper function performs three guards (each with one boolean check) and some arithmetic, but the number of checks thus drops, worst case from 723 boolean checks to 84 checks, which is likely more efficient.

I've added a testcase to check the equivalence with the "old" implementation. This had:

vty         > Test case check rgbColorToColor240 optimization is correct: Pass
jtdaugherty commented 3 years ago

Thanks for the details and for the test!