better better color sorting and legend detection