Skip to content

Commit

Permalink
feat: utils.math removed, colors hsl func removed, improv darken,lighten
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmx03 committed Dec 21, 2023
1 parent ce2ef56 commit b219df9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 222 deletions.
52 changes: 23 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,6 @@ local colorhelper = require('solarized.utils.colors')
-- Convert a hex color code to RGB
colorhelper.hex_to_rgb('#ffffff')

-- Convert RGB values to HSL
colorhelper.rgb_to_hsl(255, 255, 255)

-- Convert HSL values to RGB
colorhelper.hsl_to_rgb(0, 0, 100)

-- Darken a color by a specified percentage
colorhelper.darken('#ffffff', 100)

Expand Down Expand Up @@ -334,29 +328,25 @@ collaboration, please follow the guidelines below:
```

```bash
ok 1 - Color Conversions Convert hex to RGB
ok 2 - Color Conversions Convert RGB to hex
ok 3 - Color Conversions Convert RGB to HSL
ok 4 - Color Conversions Convert HSL to RGB
ok 5 - Color Conversions Darken the color by percentage
ok 6 - Color Conversions Lighten the color by percentage
ok 7 - Color Conversions Blend colors
ok 8 - Configuration Default configuration
ok 9 - Configuration Unique configuration table instances
ok 10 - Configuration Extend default configuration
ok 11 - Initialization Loads without encountering any errors
ok 12 - Initialization Background is set to light
ok 13 - Math Round to the nearest integer
ok 14 - Math Return the value of the first parameter and the sign of the second parameter
ok 15 - Palette Extend solarized color palette
ok 16 - Palette Correct any invalid colors when extending the solarized palette
ok 17 - Setup Customizing Highlight Groups
ok 18 - Setup Changing Comment Style
ok 19 - Setup Changing Function Style
ok 20 - Setup Customizable Highlight Groups Without Losing Previous Configuration
ok 21 - Setup Ability to Change the Default Theme

# Success: 21
ok 1 - Color Conversions Convert hex to RGB
ok 2 - Color Conversions Convert RGB to hex
ok 3 - Color Conversions Darken the color by percentage
ok 4 - Color Conversions Lighten the color by percentage
ok 5 - Color Conversions Blend colors
ok 6 - Configuration Default configuration
ok 7 - Configuration Unique configuration table instances
ok 8 - Configuration Extend default configuration
ok 9 - Initialization Loads without encountering any errors
ok 10 - Initialization Background is set to light
ok 11 - Palette Extend solarized color palette
ok 12 - Palette Correct any invalid colors when extending the solarized palette
ok 13 - Setup Customizing Highlight Groups
ok 14 - Setup Changing Comment Style
ok 15 - Setup Changing Function Style
ok 16 - Setup Customizable Highlight Groups Without Losing Previous Configuration
ok 17 - Setup Ability to Change the Default Theme
1..17
# Success: 17
```

3. **Pull Request**: When submitting a pull request, select the `dev` branch
Expand Down Expand Up @@ -385,3 +375,7 @@ Ethan Schoonover
- [tokyonight](https://github.com/folke/tokyonight.nvim)

[![Raphael](https://github.com/glepnir.png?size=100)](https://github.com/glepnir)

```
```
23 changes: 0 additions & 23 deletions lua/solarized/utils/Math.lua

This file was deleted.

149 changes: 23 additions & 126 deletions lua/solarized/utils/colors.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local Math = require('solarized.utils.Math')
local M = {}

--- Convert a hexadecimal color code to RGB color values
Expand All @@ -25,140 +24,38 @@ function M.rgb_to_hex(red, green, blue)
return string.format('#%02x%02x%02x', red, green, blue)
end

--- Converts RGB (Red, Green, Blue) color values to HSL (Hue, Saturation, Lightness) color values.
---
--- @param r number The red component of the RGB color (0-255).
--- @param g number The green component of the RGB color (0-255).
--- @param b number The blue component of the RGB color (0-255).
--- @return number h The hue value in degrees (0-360).
--- @return number s The saturation value as a percentage (0-100).
--- @return number l The lightness value as a percentage (0-100).
function M.rgb_to_hsl(r, g, b)
-- Convert RGB values to the range 0-1
r = r / 255
g = g / 255
b = b / 255

-- Find the minimum and maximum values
local min = math.min(r, g, b)
local max = math.max(r, g, b)

-- Calculate luminance (L)
local l = (max + min) / 2

-- Check if there is saturation
local s
if min == max then
-- If min and max are equal, there is no saturation
s = 0
else
if l <= 0.5 then
s = (max - min) / (max + min)
else
s = (max - min) / (2 - max - min)
end
end

-- Calculate hue (H)
local h
if max == min then
-- If max and min are equal, it's a shade of gray (H is undefined)
h = 0
else
if max == r then
h = (g - b) / (max - min)
elseif max == g then
h = 2 + (b - r) / (max - min)
else
h = 4 + (r - g) / (max - min)
end
h = h / 6 -- Convert to range 0-1
if h < 0 then
h = h + 1
end -- Add 1 if negative
end

-- Convert hue to degrees
h = h * 360

return Math.round(h), Math.round(s * 100), Math.round(l * 100)
end

--- Converts HSL (Hue, Saturation, Lightness) color values to RGB (Red, Green, Blue) color values.
---
--- @param h number The hue value in degrees (0-360).
--- @param s number The saturation value as a percentage (0-100).
--- @param l number The lightness value as a percentage (0-100).
--- @return number red The red component of the RGB color (0-255).
--- @return number green The green component of the RGB color (0-255).
--- @return number blue The blue component of the RGB color (0-255).
function M.hsl_to_rgb(h, s, l)
h = h / 360
s = s / 100
l = l / 100

local function hueToRgb(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < 1 / 6 then
return p + (q - p) * 6 * t
end
if t < 1 / 2 then
return q
end
if t < 2 / 3 then
return p + (q - p) * (2 / 3 - t) * 6
end
return p
end

if s == 0 then
local gray = Math.round(l * 255)
return gray, gray, gray
else
local q = l < 0.5 and l * (1 + s) or l + s - l * s
local p = 2 * l - q

local red = Math.round(hueToRgb(p, q, h + 1 / 3) * 255)
local green = Math.round(hueToRgb(p, q, h) * 255)
local blue = Math.round(hueToRgb(p, q, h - 1 / 3) * 255)

return red, green, blue
end
end

--- Darken a color by a given percentage
--- Darken a color by a given distance
---
--- @param color string The color to be darkened in any valid color format
--- @param percent number The percentage by which the color should be darkened (between 0 and 100)
--- @param percentage number The distance by which the color should be darkened (between 1 and 10)
--- @return string color The resulting darkened color in the same format as the input color
function M.darken(color, percent)
local r, g, b = M.hex_to_rgb(color)
local h, s, l = M.rgb_to_hsl(r, g, b)

l = l * (1 - percent / 100)

local new_r, new_g, new_b = M.hsl_to_rgb(h, s, l)
return M.rgb_to_hex(new_r, new_g, new_b)
function M.darken(color, percentage)
local red, green, blue = M.hex_to_rgb(color)
local i = 1
local result = {
red = red * (1 - (percentage / 100) * i),
green = green * (1 - (percentage / 100) * i),
blue = blue * (1 - (percentage / 100) * i),
}

return M.rgb_to_hex(result.red, result.green, result.blue)
end

--- Lighten a color by a given percentage
---
--- @param color string The color to be lightened in any valid color format
--- @param percent number The percentage by which the color should be lightened (between 0 and 100)
--- @param percentage number The percentage by which the color should be lightened (between 1 and 10)
--- @return string color The resulting lightened color in the same format as the input color
function M.lighten(color, percent)
local r, g, b = M.hex_to_rgb(color)
local h, s, l = M.rgb_to_hsl(r, g, b)

l = l + (100 - l) * (percent / 100)

local new_r, new_g, new_b = M.hsl_to_rgb(h, s, l)
return M.rgb_to_hex(new_r, new_g, new_b)
function M.lighten(color, percentage)
local red, green, blue = M.hex_to_rgb(color)
local i = 1
local result = {
red = red + (255 - red) * i * (percentage / 100),
green = green + (255 - green) * i * (percentage / 100),
blue = blue + (255 - blue) * i * (percentage / 100),
}

return M.rgb_to_hex(result.red, result.green, result.blue)
end

--- Blend two colors with a given alpha value
Expand Down
33 changes: 4 additions & 29 deletions tests/colors_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,17 @@ describe('Color Conversions', function()
assert.equals(expect, string.lower(output))
end)

test('Convert RGB to HSL', function()
local colors = solarized_palette.get_colors()
local red, green, blue = colorhelper.hex_to_rgb(colors.blue)
local h, s, l = colorhelper.rgb_to_hsl(red, green, blue)
local expect = { h = 205, s = 69, l = 49 }

assert.equals(expect.h, h)
assert.equals(expect.s, s)
assert.equals(expect.l, l)
end)

test('Convert HSL to RGB', function()
local colors = solarized_palette.get_colors()
local red, green, blue = colorhelper.hex_to_rgb(colors.blue)
local h, s, l = colorhelper.rgb_to_hsl(red, green, blue)
local r, g, b = colorhelper.hsl_to_rgb(h, s, l)
local expect = { red = red, green = green, blue = blue }

assert.are_not.equals(expect.red, r)
assert.are.equals(expect.green, g)
assert.are_not.equals(expect.blue, b)
end)

test('Darken the color by percentage', function()
local colors = solarized_palette.get_colors()
local output = colorhelper.darken(colors.green, 50)
local expect = '#424d00'

local output = colorhelper.darken(colors.green, 100)
local expect = '#000000'
assert.equals(expect, output)
end)

test('Lighten the color by percentage', function()
local colors = solarized_palette.get_colors()
local output = colorhelper.lighten(colors.green, 50)
local expect = '#e7ff4d'

local output = colorhelper.lighten(colors.green, 100)
local expect = '#ffffff'
assert.equals(expect, output)
end)

Expand Down
15 changes: 0 additions & 15 deletions tests/math_spec.lua

This file was deleted.

0 comments on commit b219df9

Please sign in to comment.