Quantcast
Channel: Rainmeter Forums
Viewing all articles
Browse latest Browse all 709

Utilities and Tools • Lua for Oklch

$
0
0
Oklch color space. Are you ready to jump in?

250326-012938.png
I have been creating a skin which randomly changes color by using HSL color space. Then I noticed, "Blue looks too dark!"

So I decided to use Oklch color space and started to write a Lua script that handles the color space.


Oklch was proposed by Björn Ottosson in 2020 and included in CSS specifications in 2021. All of major web browsers already support it. The color space takes into account how color is seen by human eyes and is designed to ensure perceptual uniformity. I believe that it is the most advanced color space available now.

You might want to visit Ottosson's web page if you get interested in more details about Oklch. There are also many other web sites explaining the strengths and advantages of the color space. You can find them by googling Oklch.


After tackling a series of bugs and mysterious phenomena, I think I finally finished the job. So I want to share the code with you all in this community. I also need to enlist your cooperation to detect bugs that might be lurking in the code.

How to use the skin
Click the input field, input a color code, and press the Enter key. You will see color circles and gradient bars in both Oklch and HSL color spaces based on the color you selected. The input field also accepts alpha components or transparency values. Located in the upper right corner is the icon which toggles between light and dark modes.

The skin is scalable. You can enlarge or shrink the skin to any size by changing the value of the "scaleFactor" variable at the Variables section in the .ini file.

Resources:
Contents of the Lua scripts
oklch_color.lua

This file includes the following functions that handle Oklch color space:
  • Get_LCH(color, specifier): Gets LCH values of an RGB color
    The second argument is optional. If the argument is either 'L', 'C', 'H', or 'A', the function returns only one value specified by the argument.
  • Set_LCH(color, L, C, H, A): Sets LCH values of an RGB color to specified values
    In the LCH values, a letter '*' can be used as a placeholder which means "Do nothing."
  • Shift_LCH(color, L, C, H, A): Shifts LCH values of an RGB color by specified amounts
    Like the above function, a letter '*' can be used as a placeholder instead of the LCH values.
The first argument color is a string that represents an RGB color. Both decimal and hexadecimal formats are accepted. In the second and third functions, using a letter '*' allows only specific values to be changed. Alpha components of color are optional in all these functions.

These functions can also be called directly from the skin's .ini file by using inline Lua.

That's it. Basically you should be able to manipulate color just by using these three functions. Lower level functions are also provided which allow more meticulous control of color. The example skin uses these lower level functions. The description of these functions are included in the file.

hsl_color.lua

This file includes HSL counterparts of the above functions. The description is omitted here because it is out of the scope of this post. Read the file for details.

hsv_color.lua

This file includes HSV counterparts of the above functions. The description is omitted here because it is out of the scope of this post. Read the file for details.

This file is not used anywhere. It is provided just for somebody who wants to use it and just in case someday I will want to use it.
Typical usage of the scripts
At first, include the .lua file into your Lua script.

Code:

function Initialize()    dofile('<path to the file>')    -- other processes --    returnend
Then, use the functions above in any of your functions as follows (this snippet assumes that hue is shifted):

Code:

function someFunction()    -- import a color string from the skin's .ini file    -- for example    local color = SKIN:GetMeter('<MeterName>'):GetOption('<OptionName>')    -- set the shift amount of hue    local H = <someValue>        -- shift hue    local rgb_str = Shift_LCH(color, '*', '*', H)        -- apply the string to the skin's .ini file    -- for example    SKIN:Bang('!SetOption', '<MeterName>', '<OptionName>', rgb_str)    returnend
I hope the code will be of any help to you. Explore what the new color space has to offer!

Download:
Lua for Oklch_1.0.0.rmskin

Statistics: Posted by PineYellow — Yesterday, 5:29 pm — Replies 1 — Views 43



Viewing all articles
Browse latest Browse all 709

Trending Articles