Skip to content

Commit

Permalink
release 5.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Jan 22, 2024
1 parent bfc41c1 commit 84fca53
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change logs

## 2024.01.22 v5.17.0

1. `randomColor` supports a range of custom color values, which can be used to customize the generation of dark, light, warm, etc.

## 2024.01.21 v5.16.0

1. new `safeParse` `safeStringify` function
Expand Down
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,42 @@ declare function dash2Camel(string: string): string

Generate random hexadecimal colors

> Support for custom color value ranges starting with version 5.17.0, which can be used to customize the generation of darker, lighter, warmer colors, etc.

- Since: `5.5.0`

- Arguments: none
- Arguments:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | --------------------------------------- | -------------------------------------- | -------- | -------- | ------- |
| min | the minimum value of the random numbers | `number` \/ `[number, number, number]` | - | `false` | - |
| max | the maximum value of the random numbers | `number` \/ `[number, number, number]` | - | `false` | - |

- Returns: `string`

- Example:

```ts
randomColor() // #ff6600
randomColor()
// #bf444b
randomColor(200)
// #d6e9d7
randomColor(200, 255)
// #d3f9e4
randomColor([0, 0, 0], [255, 255, 255])
// #e2f2f3
```

- Types:

```ts
declare function randomColor(): string
declare function randomColor(
min?: number | [number, number, number],
max?: number | [number, number, number]
): string
```

#### randomNumber
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "js-cool",
"description": "Collection of common JavaScript / TypeScript utilities",
"version": "5.16.0",
"version": "5.17.0",
"packageManager": "pnpm@8.9.2",
"main": "dist/index.cjs.js",
"module": "dist/index.esm-bundler.js",
Expand Down
38 changes: 36 additions & 2 deletions src/randomColor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
import randomNumber from './randomNumber'

/**
* Generate random hexadecimal colors
*
* @example
* ```js
* randomColor()
* // #bf444b
*
* randomColor(200)
* // #d6e9d7
*
* randomColor(200, 255)
* // #d3f9e4
*
* randomColor([0, 0, 0], [255, 255, 255])
* // #d6e9d7
* ```
* @since 5.5.0
* @param min - the minimum value of the random numbers, eg: [10, 10, 10]
* @param max - the maximum value of the random number, eg: [255, 255, 255]
* @returns - result
*/
function randomColor(): string {
return `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`
function randomColor(
min?: number | [number, number, number],
max?: number | [number, number, number]
): string {
if (!max && !min && min !== 0)
return `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`

let min1, min2, min3, max1, max2, max3

if (!min) min1 = min2 = min3 = 0
else if (typeof min === 'number') min1 = min2 = min3 = min
else [min1, min2, min3] = min

if (!max) max1 = max2 = max3 = 255
else if (typeof max === 'number') max1 = max2 = max3 = max
else [max1, max2, max3] = max

return `#${randomNumber(min1, max1).toString(16).padStart(2, '0')}${randomNumber(min2, max2).toString(16).padStart(2, '0')}${randomNumber(min3, max3).toString(16).padStart(2, '0')}`
}

export default randomColor

0 comments on commit 84fca53

Please sign in to comment.