Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color manipulation methods #2136

Merged
merged 5 commits into from
Jan 23, 2025
Merged

Color manipulation methods #2136

merged 5 commits into from
Jan 23, 2025

Conversation

Be1zebub
Copy link
Contributor

Features:
HWB support.
Color manipulation methods with HSV, HSL, HSB, HWB.

The main way to use this methods is to create colors for the UI.
For example, the Discord color scheme consists of the same hue and saturation, they just change lightness to create a beautiful color palette.

All you need to make pallete is background color, text color & accent color.
So addons theme config can be just 3-5 colors, instead of:
image

Demo:

hl2_ga8aLyIIvR.mp4

demo src: https://gist.github.com/Be1zebub/a015958b71eb8806f0ce5db3e55ed4c0

Add color manipulation functions with HSV, HSL, HSB, HWB.
Add HWB support.
@robotboy655 robotboy655 added the Addition The pull request adds new functionality. label Oct 14, 2024
@robotboy655
Copy link
Collaborator

robotboy655 commented Jan 21, 2025

Do you think it's also worth porting HSVToColor and HSLToColor to Lua? Not sure how the performance would differ, but it would solve Facepunch/garrysmod-issues#2407

Also there are conflicts that must be resolved.

@Be1zebub
Copy link
Contributor Author

Be1zebub commented Jan 21, 2025

is this legit? lua implementation 100500% faster
hsl2color is same results
image
https://gist.github.com/Be1zebub/e069b63ca4b96d16ed99fd77af2ae9aa

fix style, rm :SetValue alias, add HSLToColor & HSVToColor lua implementation (100500x faster than C)
@Be1zebub
Copy link
Contributor Author

ready 2 merge
image

Moved stuff around to be more logically placed, made changes to HSVToColor and HSLToColor to more closely match the C++ version's returns
Copy link
Collaborator

@robotboy655 robotboy655 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HSVToColor and HSLToColor replacements are not 1 to 1 with the C++ versions, sometimes generating an off by 1 difference (I think this is just floating point math discrepancies), and severely different when given invalid inputs.

The c++ code generates the same negative values with invalid inputs, but get changed to correct range via either float to int cast, or int to unsigned char cast.

I have applied some changes that gets it close, but not exactly the same. (off by 2)

@Be1zebub Be1zebub closed this Jan 23, 2025
@Be1zebub Be1zebub deleted the patch-10 branch January 23, 2025 01:03
@Be1zebub Be1zebub restored the patch-10 branch January 23, 2025 01:04
@Be1zebub Be1zebub reopened this Jan 23, 2025
255 % 255 == 0
we cant use mod here
@Be1zebub
Copy link
Contributor Author

Be1zebub commented Jan 23, 2025

mod leads to unexpected results.
eg hsv 80, 1, 1 gives rgb 170, 0, 0 instead of rgb 170, 255, 0
255 % 255 == 0
better to use math.min/math.max in this case

it looks like c++ also uses mod, which is incorrect
eg C++ HSVToColor(80, 0, 1.1) gives 24, 24, 24
while lua HSVToColor(80, 0, 1.1) that using formulas from wikipedia gives 255, 255, 255 which corresponds to

if saturation == 0 then -- grayscale, depends only on value
    rgb = math.floor(value * 255) -- 280.5 (255 floor)
end

c++ 24 is like 280.5 % 255 which is 25.5 with weird rounding that you are noticed.

c++ HSVToColor(-1, 1, -1) results is 1, 4, 0 while should be 0, 0, 0 since value (brightness) is <= 0 - now it doesnt look like just a floating-point precision issue, more like error in c++ formula.

@robotboy655 robotboy655 merged commit ee0b187 into Facepunch:master Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Addition The pull request adds new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants