Skip to content

Commit

Permalink
Add color util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski committed Dec 19, 2023
1 parent 97a3492 commit 8ffe7f3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./src/colors/colors"
export * from "./src/errors/errors"
export * from "./src/events/events"
export * from "./src/format/format"
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@curiousleaf/utils",
"description": "A lightweight set of utilities",
"version": "1.0.6",
"version": "1.0.7",
"license": "MIT",
"module": "./dist/index.js",
"type": "module",
Expand All @@ -16,7 +16,9 @@
"scripts": {
"build": "bun build ./index.ts --outdir ./dist"
},
"dependencies": {},
"dependencies": {
"@uiw/color-convert": "1.4.3"
},
"devDependencies": {
"@biomejs/biome": "latest",
"bun-types": "latest",
Expand Down
16 changes: 16 additions & 0 deletions src/colors/colors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "bun:test"
import { isLightColor } from './colors'

describe('isLightColor', () => {
it('should return true for a light color', () => {
const hexa = '#FFFFFF'
const result = isLightColor(hexa)
expect(result).toBe(true)
})

it('should return false for a dark color', () => {
const hexa = '#000000'
const result = isLightColor(hexa)
expect(result).toBe(false)
})
})
17 changes: 17 additions & 0 deletions src/colors/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { hexToRgba } from "@uiw/color-convert"


export * from "@uiw/color-convert"

/**
* Check if a given color in hexadecimal format is a light color.
*
* @param hexa - The hexadecimal color code to check.
* @returns A boolean indicating if the color is light.
*/
export const isLightColor = (hexa: string) => {
const { r, g, b, a } = hexToRgba(hexa)
const brightness = r * 0.299 + g * 0.587 + b * 0.114 + (1 - a) * 255

return brightness > 186
}
19 changes: 9 additions & 10 deletions src/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {
capitalize,
convertNewlines,
getExcerpt,
getInitials,
isCuid,
isTruthy,
range,
stripHtml,
toSlugCase,
toTitleCase,
capitalize,
convertNewlines,
getExcerpt,
getInitials,
isCuid,
isTruthy,
range, stripHtml,
toSlugCase,
toTitleCase
} from "./helpers"
import { describe, expect, it } from "bun:test"

Expand Down
14 changes: 8 additions & 6 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
* A collection of helper functions used throughout the application.
*/

/**
* Generates an array of numbers
* @param start The start of the range
* @param end The end of the range
* @returns An array of numbers
*/
/**
* A utility function that generates an array of numbers within a specified range.
* @param start - The starting number of the range.
Expand All @@ -20,6 +14,14 @@ export const range = (start: number, end: number) => {
return Array.from({ length }, (_, idx) => idx + start)
}

/**
* Delays the execution of the function by the specified amount of time.
* @param delay - The amount of time to delay the execution of the function, in milliseconds.
*/
export const sleep = async (delay: number) => {
new Promise((resolve) => setTimeout(resolve, delay))
}

/**
* Capitalizes the first letter of a string.
* @param value - The string to capitalize.
Expand Down

0 comments on commit 8ffe7f3

Please sign in to comment.