Mini-Sylar / Server-Client-ChatApp

Client Server chat app using python sockets and pyqt5
MIT License
12 stars 6 forks source link

Implement function to randomly generate dim to semi-bright colors #13

Closed Mini-Sylar closed 2 years ago

yanbax commented 2 years ago

Hi!

I'd like to contribute if you're open to it, but would need a bit more info on what you're looking for here?

Mini-Sylar commented 2 years ago

Hi!

I'd like to contribute if you're open to it, but would need a bit more info on what you're looking for here?

hello! ok so the issue is I have this code snippet that generates a random color in hex that I assign to each chat bubble of a person that joins the chat group

def rand_color(def_color='#a5d6a7'):
    r = lambda: random.randint(0, 255)
    return '#%02X%02X%02X' % (r(), r(), r()) if r else def_color

the problem is that, it sometimes generates darker colors which makes the message content unreadable, i'm looking for a better way to approach this issue as currently messages are black text on light backgrounds

yanbax commented 2 years ago

Hey! See above - forked and had a look at this my side, think the code in that pull request should be nearer to what you want.

I changed from the lambda function you had previously which generates RGB values into a function which first generates HSL values.

As HSL specifically pinpoints the 'lightness' of colours, I just used a random.uniform() pick method; lightness is expressed between 0 and 1 with 0 being closer to black and 1 closer to white. I just asked it to pick an l value between 0.5 and 1 so we can be sure the generated colour is on the brighter side.

I additionally set the lowest value of the saturation (s) to 0.2 - below this the colours get a bit murky and tend to be more grey, but obviously feel free to play with it and adjust to how you see fit.

It's a bit dirty from there as I decided to keep the final value of the function as a HEX to make sure it didn't break anything further down the line - I kept your original implementation to convert RGB to HEX, as I couldn't find a way to convert directly from HSL. As such, we now convert twice - HSL -> RGB -> HEX.

NB: I tested the rand_color function separately as I wasn't able to get the full ChatApp running on my machine for some reason (line 32 in Server.py hangs, I'm still looking into why that is), so I hope it works! I figured this was a quick win which I could probably do without running the full test, but of course those are 'famous last words'...

Mini-Sylar commented 2 years ago

Right!! you can control lightness using HSL! how did I miss that! makes so much sense

Anyway, I tested. The new _randcolor generates lighter colors that match the black perfectly. I'm sure we can look into how to convert directly from HSL to HEX down the line but for now this works just as I wanted 👍. Your explanation was really clear

Concerning the Line 32 in the server. I forgot to add a print message showing that the server is running. So it was probably running. You should only see something pop up if ClientCode is run and you've connected unless there was a specific error you got

The only problem I know exists is the path to the other modules since they're in different folders. Pycharm is able to find them but running on desktop throws errors. This could also have been the issue.

Screenshot added HSL_Update

yanbax commented 2 years ago

Awesome, I'm so glad it worked well :0)

Noted on the server messaging! I will have another go.

If there's anything else you'd like help on feel free to chuck it my way - I'm just graduating from a degree so have a lot of time on my hands over the summer!

Mini-Sylar commented 2 years ago

Oh nice. I'll be graduating in September sadly

also as a fix to the code not working if you run it locally without any IDE

I just pushed the update to fix that