Adjust the brightness of a specified oklch color by 50% using CSS oklch(from ...) in combination with the calc() function

We have implemented a design system that includes various CSS custom properties for colors. For example:

:root {
  --color--blue-1: oklch(88.6% 0.023 238);
}

Our latest addition is the utilization of the oklch color format.

Currently, I am tasked with calculating a color that is 50% lighter using the oklch(from...) function in CSS without incorporating opacity or alpha to maintain a solid color appearance regardless of the background.

The expected outcome would be as follows:

:root {
  --color-blue-1-50: oklch(94.2% 0.023 238);
}

The formula for achieving this is straightforward: simply identify 'l' from 'lch,' then the new 'l_new' would be calculated by adding half of the difference between the original value and 100% to the 'l.'

l_new = l + ((100% - l) / 2)

In order to include this calculation in a CSS calc function within our code snippet like this:

:root {
  --color-blue-1-50: oklch(from var(--color-blue-1) calc(/* these are the missing bits */) c h);
}

I attempted to implement this, but it resulted in an invalid value being displayed:

:root {
  --color-blue-1-50: oklch(from var(--color-blue-1) calc(l + calc(100% - l / 2)) c h);
}

:root {
  --color-blue-1: oklch(94.2% 0.023 238);
  --color-blue-1-50: oklch(from var(--color-blue-1) calc(l + (100% - l) / 2) c h);
}

div {
  display: block;
  height: 100px;
  width: 100px;
}

.original {
  background-color: var(--color-blue-1);
}

.fifty-percent-lighter {
  background-color: var(--color-blue-1-50);
}

.faked {
  opacity: 0.5;
}
<div class="original">original</div>
<div class="fifty-percent-lighter">50% lighter</div>
<div class="original faked">50% lighter using opacity</div>

Answer №1

A different approach to your equation would be to consider it as (l/2 + 50%) and replace 50% with .5

:root {
  --color-blue-1: oklch(80.2% 0.023 238);
  --color-blue-50: oklch(from var(--color-blue-1) calc(l/2 + .5) c h);
}

div {
  display: block;
  height: 100px;
  width: 100px;
}

.original {
  background-color: var(--color-blue-1);
}

.fifty-percent-lighter {
  background-color: var(--color-blue-50);
}

.faked {
  opacity: 0.5;
}
<div class="original">original</div>
<div class="fifty-percent-lighter">50% lighter</div>
<div class="original faked">50% lighter using opacity</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why isn't the alignment and background color of my navigation menu (nav) functioning properly?

Having trouble with my navigation styling - the alignment and background color only show up when I remove the closing brace for the "nav" CSS. I have 2 different CSS stylings in place to center the nav items and add a background color, but something's ...

Problem with CSS Classes Styling

Having an issue with CSS in Windows Notepad: <!DOCTYPE html> <html> <head> <title>UniqueScience</title> <style> #title { display: block; ...

Using AngularJS to apply custom css to a tag within a directive for creating a Bootstrap sticky footer

Currently, I am in the process of developing my very first AngularJS application with Bootstrap as the responsive framework. In order to achieve a sticky footer, I usually utilize jQuery to determine the outerHeight of the footer and then apply that value ...

Steps for including a header and footer with page numbers in an HTML page without disrupting the rest of the content when printing

When I print a page with a table that spans multiple pages, I want to ensure that my header and footer are included on every page. The code I've tried so far has treated the entire content as one continuous page. How can I achieve this? ...

implement an angular directive to apply a CSS element

I am utilizing AngularJS and ng-repeat to populate a dynamic list of studies. This list has the capability to toggle into child elements of each item, creating an accordion-style toggle list that can go up to three levels deep for each list item. I am curr ...

Reorganizing DIV elements on-the-fly

I've recently delved into the world of html, css, and javascript with the hopes of creating my very own news website. However, I've come across a puzzling problem: My vision is to have 10 div blocks on the homepage, each containing a headline wi ...

Is there a way to pass locale data using props in VueJS Router?

To access hotel data, the URL path should be localhost:8080/hotel/:id (where id is equal to json.hoteID). For example, localhost:8080/hotel/101 This path should display the specific data for that hotel. In order to achieve this, we will utilize VueJS vu ...

Easily transition the header's height when selecting a menu item

I am trying to achieve a collapse effect or something similar However, when I click on a menu item, the height changes too quickly without any animation What could be wrong in my code? I am looking to reference an effect similar to the one seen in the h ...

Why does this inner HTML table always adjust its width based on the content within it? Is there a way to make it match the width of its container instead

I'm not very familiar with HTML and CSS, and I've come across a problem. Here is the CSS structure in question: <!-- TECHNICAL REFERENCE: --> <div id="referenteTecnicoTab"> <table width="800px" class="standard-table-cls table-he ...

What is the best way to position an image next to an input element?

How can I position submit.png next to the input element in my code? <div class="input-box" style="height: 40px; width: 500px;"> <img src="img/delete.png" width="25px" height="25px;" style="position: absolute; right: 0;"> ...

HTML5 template design clashes with Smarty framework

I've been working with a simple pattern like this: <input type="text" pattern="[a-z]{2}" required ../> However, it never seems to be valid. The pattern doesn't seem to work as expected. I have only tested it in Firefox so far. Is there ...

Shift icon position to the right using CSS

I designed a user interface that includes an icon, user name, and phone number. The CSS properties I applied to the image are as follows: float: left; position: relative; margin-left: 4%; When I increase the margin-left value, both the image and username ...

Trouble with Displaying HTML Table in Bootstrap 4.3.1 Tooltip

Struggling for hours to set up a custom HTML table in a tooltip box, I finally found that the Bootstrap version solves the issue. Surprisingly, it works perfectly under Bootstrap 4.0.0 but fails under Bootstrap 4.3.1. Could this be a bug or am I overlooki ...

Issue with floating the navbar to the right in Bootstrap 4

I am trying to reposition the navbar (without logo) to the right side using code. To achieve this, I included the class "float-right" like so <div class="collapse navbar-collapse float-right" id="navbarSupportedContent">. However, this modification ...

What is the best way to apply background color to match the height of the parent element?

When creating cards, I encounter a challenging issue. I am struggling to fill the background-color of the entire box content as I am new to HTML and CSS and unsure how to achieve this. Below are my codes. .box-container1 { display: flex; justify-con ...

When attempting to achieve a CSS percent width, the output is given in pixels instead. I am in search of a solution where the percentage remains the same as the

My styling is set as a percentage, like this: <style> #somediv {width:70%} </style> <div id="somediv"></div> jQuery's css() function returns the result in pixels $(document).ready(function(){ var css = $("#somediv").css( ...

What is the best way to incorporate CSS from node_modules into Vite for production?

I have a TypeScript web application where I need to include CSS files from NPM dependencies in index.html. Here is an example of how it is done: <link rel="stylesheet" type="text/css" href="./node_modules/notyf/notyf.min.css&quo ...

Utilizing CSS pseudo-elements in selectors with Scrapy

Is it possible to use the CSS text-transform property directly within Scrapy selectors? Ex: .css("::text-transform: capitalize").extract() The current method returns capitalized data, but can we apply CSS properties like text-transform to modify ...

I am looking to save the session value into the search box of the appTables application by utilizing jQuery

Is there a way to save the session value in the search box of appTables by using jQuery? <?php $session = \Config\Services::session(); ?> <script type="text/javascript"> $(document).ready(function () { var searc ...

Is there a way to incorporate CSS or tables when converting HTML to PDF using JavaScript?

While working on a project, I successfully converted an HTML file into a PDF. However, the output did not display the CSS design. Can anyone provide suggestions on how to include CSS design in the PDF file? Below is the JavaScript function code: $(funct ...