[Python] strip hex to a single digit in string formating in python

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to convert RGB values to short hex format, I have the long version:

    '#{:02x}{:02x}{:02x}'.format(r, g , b)


    is it possible to get #fff instead of #ffffff without formatting the values one by one to take the first digits?

    Edit: my code doesn't require much accuracy in colors so it accepts a shortened version of color hex code that each color is represented by a single digit and the second digit is omitted.

    any value from 0 to 15 is considered 0

    any value from 16 to 31 is considered 1

    and so on...

    Continue reading...

Compartilhe esta Página