From b219df9fe366882107f590d684374c1365409b11 Mon Sep 17 00:00:00 2001 From: Max Miliano Date: Thu, 21 Dec 2023 13:34:55 -0300 Subject: [PATCH] feat: utils.math removed, colors hsl func removed, improv darken,lighten --- README.md | 52 +++++------- lua/solarized/utils/Math.lua | 23 ----- lua/solarized/utils/colors.lua | 149 +++++---------------------------- tests/colors_spec.lua | 33 +------- tests/math_spec.lua | 15 ---- 5 files changed, 50 insertions(+), 222 deletions(-) delete mode 100644 lua/solarized/utils/Math.lua delete mode 100644 tests/math_spec.lua diff --git a/README.md b/README.md index 5d649f1..17527cb 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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) + +``` + +``` diff --git a/lua/solarized/utils/Math.lua b/lua/solarized/utils/Math.lua deleted file mode 100644 index acc1b35..0000000 --- a/lua/solarized/utils/Math.lua +++ /dev/null @@ -1,23 +0,0 @@ -local M = {} - -function M.copysign(x, y) - if y > 0 or (y == 0 and math.atan2(y, -1) > 0) then - return math.abs(x) - else - return -math.abs(x) - end -end - -function M.round(x) - local absx, y - absx = math.abs(x) - y = math.floor(absx) - - if absx - y >= 0.5 then - y = y + 1.0 - end - - return M.copysign(y, x) -end - -return M diff --git a/lua/solarized/utils/colors.lua b/lua/solarized/utils/colors.lua index c1177e2..b5d3ba1 100644 --- a/lua/solarized/utils/colors.lua +++ b/lua/solarized/utils/colors.lua @@ -1,4 +1,3 @@ -local Math = require('solarized.utils.Math') local M = {} --- Convert a hexadecimal color code to RGB color values @@ -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 diff --git a/tests/colors_spec.lua b/tests/colors_spec.lua index 4d15d02..da6f034 100644 --- a/tests/colors_spec.lua +++ b/tests/colors_spec.lua @@ -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) diff --git a/tests/math_spec.lua b/tests/math_spec.lua deleted file mode 100644 index 23a1b49..0000000 --- a/tests/math_spec.lua +++ /dev/null @@ -1,15 +0,0 @@ -describe('Math', function() - local Math = require('solarized.utils.Math') - - test('Round to the nearest integer', function() - assert.equals(6, Math.round(5.76543)) - assert.equals(3, Math.round(2.5)) - assert.equals(1, Math.round(1.3)) - end) - - test('Return the value of the first parameter and the sign of the second parameter', function() - assert.equals(-4.0, Math.copysign(4, -1)) - assert.equals(8, Math.copysign(-8, 97.21)) - assert.equals(-43.0, Math.copysign(43, -76)) - end) -end)