Skip to content
Grav 2.0 is officially stable. Read the announcement →
Support

is it possible to save a colorpicker value as oklch? how do I go about adding that?

form theme

Solved by Vadym View solution

Started by Anna 4 weeks ago · 9 replies · 311 views
4 weeks ago

I have a theme that lets users pick a colour, bur I would like to have them as oklch values instead of hexadecimal. The Grav documentation says nothing about such an option, but I would like to make sure before I go hacking that…

If there is indeed no built in option for that, this would be my plan:

  • add a php library with colour conversion functions to my theme
  • hook into onAdminSave
  • convert hex to oklch
  • save with the new value

Any ideas would be much appreciated!

4 weeks ago

I have a theme that lets users pick a colour, bur I would like to have them as oklch values instead of hexadecimal.

Just curious, a user picks a color and the colorpicker returns a hex (or sRGB). What would be the benefit to convert it to oklch to store it?

4 weeks ago

It’s easier to use in Tailwind as that stores all its colours as oklch…

4 weeks ago

Tailwind provides a default color set defined using oklch. For example --color-gray-50: oklch(0.984 0.003 247.858), whould be used in css like: color: var(--color-gray-50);

I don't see why you cannot override the color variable using --color-gray-50: #FF00FF, where #FF00FF is derived from a page header variable or other variable.

I might be missing some requirement in your project though...

3 weeks ago

Oh, I totally can. Sorry, I did not explain this very well: I actually manipulate the picked theme colour into different shades, and that is very easy to do if it’s oklch, but not if I’m working with hex codes…

3 weeks ago

I actually manipulate the picked theme colour into different shades

That's the info that was missing...

I've not found a single reference to OKLCH in the entire getgrav github repo (core, admin, plugins, themes). Guess you'll need to search for an OKLCH lib and import it in your js/typescript code. E.g. https://oklume.com/ or https://oklch.com/

3 weeks ago

Hm, I would like to do it with PHP whenever the theme settings are saved. Just convert the freshly picked colour and save it in the theme config as an extra field. Do you see any reason to use js for that?

Currently my problem is that since I would like to do that onAdminSave, I need to figure out how to hook into the new Admin bits. But I’m making a new post about that, others may well have similar problems.

3 weeks ago

whenever the theme settings are saved

Ah, you want to set the color in Admin and handle it in a plugin/theme.

I need to figure out how to hook into the new Admin bits.

Read all about that in https://github.com/getgrav/grav-plugin-api

You can still use the old onAdminSave Admin-Compatible Events, but you might consider getting used to the new way of catching API calls.

3 weeks ago Solution

Hi @Netzhexe, agreeing with @pamtbaau thoughts, if you just need color manipulation (lighter/darker shades), it's much easier to save a standard HEX and handle it using modern CSS (color-mix() or oklch(from var(--hex) ...)), or the Color Tools Plugin in Twig (Typhoon theme uses it as dependency). However, a custom colorpicker would also be a great solution.

3 weeks ago

oklch(from var(--hex) ...))

Ooooooohhh I did not realise I could do that! Thank you SO much 🙏🙏🙏 I will go with that for now!

👍 1

Suggested topics

Topic Participants Replies Views Activity
Support · by Anna, 1 week ago
4 335 11 hours ago
Support · by gtx, 2 weeks ago
4 331 1 week ago
Support · by Paul Hodges, 3 weeks ago
13 378 2 weeks ago
Support · by Anna, 4 weeks ago
9 434 2 weeks ago
Support · by TomW, 3 weeks ago
4 276 3 weeks ago