Skip to content

Commit dcebd34

Browse files
authored
feature(v5.0.0): hex to grb returns undefined instead of null (#21)
* feature: hex to grb returns undefined instead of null * chore: bump deps * ci: add CI * ci: add bin * chore: fix comments, add rabbit config
1 parent a9cb6b6 commit dcebd34

14 files changed

+2195
-258
lines changed

.coderabbit.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
2+
language: "en-US"
3+
reviews:
4+
poem: false
5+
review_status: false
6+
auto_review:
7+
enabled: true
8+
drafts: false

.eslintrc

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"extends": [
3-
"plugin:@typescript-eslint/recommended",
4-
"prettier"
5-
],
6-
"plugins": ["@typescript-eslint"],
7-
"parser": "@typescript-eslint/parser",
2+
"extends": ["@allthings/eslint-config"],
83
"rules": {
94
"@typescript-eslint/explicit-function-return-type": "off"
105
}

.github/PULL_REQUEST_TEMPLATE.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This PR implements ...

.github/workflows/ci.yaml

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: pipeline
2+
on: [ push ]
3+
4+
env:
5+
TAG_NAME: $(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9._-]/_/g')
6+
NVM_NODE_VERSION: 18
7+
PROJECT: colors
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
build-and-lint:
15+
runs-on:
16+
group: default-runners
17+
labels: self-hosted
18+
steps:
19+
- uses: actions/checkout@v4
20+
env:
21+
CURRENT_RUNNER: ${{ runner.name }}
22+
23+
- uses: actions/setup-node@v4
24+
with:
25+
node-version: ${{ env.NVM_NODE_VERSION }}
26+
27+
- name: build-and-lint
28+
shell: bash
29+
run: |
30+
./bin/node-version-manager.sh yarn install --production=false --frozen-lockfile
31+
./bin/node-version-manager.sh yarn build
32+
./bin/node-version-manager.sh yarn lint
33+
34+
dry-run-publish:
35+
needs: [ build-and-lint ]
36+
runs-on:
37+
group: default-runners
38+
labels: self-hosted
39+
steps:
40+
- uses: actions/checkout@v4
41+
env:
42+
CURRENT_RUNNER: ${{ runner.name }}
43+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
44+
45+
- uses: actions/setup-node@v4
46+
with:
47+
node-version: ${{ env.NVM_NODE_VERSION }}
48+
49+
- name: dry-run-publish
50+
shell: bash
51+
run: |
52+
./bin/node-version-manager.sh yarn install --production=false --frozen-lockfile
53+
./bin/node-version-manager.sh yarn build
54+
./bin/node-version-manager.sh npm publish --access public --tag latest --dry-run
55+
56+
publish-npm:
57+
needs: [ dry-run-publish ]
58+
# TODO: not working yet
59+
if: false
60+
# if: github.ref == 'refs/heads/master'
61+
runs-on:
62+
group: default-runners
63+
labels: self-hosted
64+
steps:
65+
- uses: actions/checkout@v4
66+
env:
67+
CURRENT_RUNNER: ${{ runner.name }}
68+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
69+
70+
# Setup .npmrc file to publish to npm
71+
- uses: actions/setup-node@v4
72+
with:
73+
node-version: ${{ env.NVM_NODE_VERSION }}
74+
75+
- name: publish-npm
76+
shell: bash
77+
run: |
78+
./bin/node-version-manager.sh yarn install --production=false --frozen-lockfile
79+
./bin/node-version-manager.sh yarn build
80+
./bin/node-version-manager.sh npm publish --access public --tag latest

bin/node-version-manager.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC1090
3+
source ~/.bashrc
4+
export NVM_DIR=~/.nvm
5+
source ~/.nvm/nvm.sh
6+
source ~/.nvm/bash_completion
7+
{
8+
nvm install "v$NVM_NODE_VERSION" --lts
9+
nvm alias default "v$NVM_NODE_VERSION"
10+
nvm alias node "v$NVM_NODE_VERSION"
11+
nvm alias system "v$NVM_NODE_VERSION"
12+
nvm use "v$NVM_NODE_VERSION"
13+
npm install -g yarn
14+
15+
"$@"
16+
} || {
17+
echo "An error occurred during execution."
18+
exit 1
19+
}

package.json

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
"name": "@allthings/colors",
33
"author": "Allthings",
44
"license": "MIT",
5-
"version": "4.0.2",
5+
"version": "5.0.0",
66
"files": [
77
"dist"
88
],
99
"main": "dist/index.js",
1010
"devDependencies": {
11-
"@typescript-eslint/eslint-plugin": "^5.58.0",
12-
"@typescript-eslint/parser": "^5.58.0",
13-
"eslint": "^8.38.0",
14-
"eslint-config-prettier": "^8.8.0",
15-
"prettier": "^2.8.7",
16-
"rimraf": "^5.0.0",
17-
"typescript": "^5.0.4"
11+
"@allthings/eslint-config": "^2.1.1",
12+
"eslint": "^8.57.0",
13+
"prettier": "^3.3.2",
14+
"rimraf": "^5.0.7",
15+
"typescript": "^5.5.2"
1816
},
1917
"prettier": {
2018
"semi": false,

renovate.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"extends": ["@allthings", ":preserveSemverRanges"]
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": ["github>allthings/renovate-config", "docker:disable"]
34
}

src/ColorPalette.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable sort-keys */
12
const palette = {
23
lightBlack: '#2c2c2c',
34
lightBlackIntense: '#232323',
@@ -49,12 +50,14 @@ const background = {
4950
white: palette.white,
5051
none: palette.none,
5152
}
53+
5254
const text = {
5355
primary: '#333333',
5456
secondary: '#626262',
5557
gray: '#bbbbbb',
5658
lightGray: '#e7ecee',
5759
}
60+
5861
const primary = {
5962
gray: palette.lightGreyIntense,
6063
blue: palette.blue,
@@ -63,11 +66,13 @@ const primary = {
6366
red: palette.brownIntense,
6467
orange: palette.yellowOrangeIntense,
6568
}
69+
6670
const state = {
6771
success: palette.mintIntense,
6872
warning: palette.yellowOrangeIntense,
6973
error: palette.redIntense,
7074
}
75+
7176
const social = {
7277
facebook: '#3b5998',
7378
google: '#c53929',

src/alpha.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
import hexToRgb from './hexToRgb'
22
import rgbToHex from './rgbToHex'
33

4-
export interface RGBColor {
4+
/* eslint-disable sort-keys, typescript-sort-keys/interface */
5+
export interface IRGBColor {
56
r: number
67
g: number
78
b: number
89
}
910

10-
function calculateTransparentColor(color: RGBColor, alpha: number) {
11+
const calculateTransparentColor = (color: IRGBColor, alphaNumber: number) => {
1112
const blendingColor = {
1213
r: 255,
1314
g: 255,
1415
b: 255,
1516
}
1617

1718
return rgbToHex(
18-
(1 - alpha) * blendingColor.r + alpha * color.r,
19-
(1 - alpha) * blendingColor.g + alpha * color.g,
20-
(1 - alpha) * blendingColor.b + alpha * color.b,
19+
(1 - alphaNumber) * blendingColor.r + alphaNumber * color.r,
20+
(1 - alphaNumber) * blendingColor.g + alphaNumber * color.g,
21+
(1 - alphaNumber) * blendingColor.b + alphaNumber * color.b,
2122
)
2223
}
2324

24-
export default function alpha(
25+
const alpha = (
2526
hex: string,
26-
alpha: number,
27+
alphaNumber: number,
2728
actualColor?: boolean,
28-
): string {
29+
): string => {
2930
const rgb = hexToRgb(hex)
30-
if (rgb === null) {
31+
if (rgb === undefined) {
3132
throw new Error(
3233
`\`hex\` value provided to \`alpha\` is not a HEX color, \`${hex}\` given.`,
3334
)
3435
}
3536

3637
return actualColor
37-
? calculateTransparentColor(rgb, alpha)
38-
: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})`
38+
? calculateTransparentColor(rgb, alphaNumber)
39+
: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alphaNumber})`
3940
}
41+
42+
export default alpha

src/hexToRgb.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { RGBColor } from './alpha'
2-
3-
export default function hexToRgb(hex: string): RGBColor | null {
1+
/* eslint-disable sort-keys */
2+
const hexToRgb = (
3+
hex: string,
4+
): { b: number; g: number; r: number } | undefined => {
45
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
56
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
6-
hex = hex.replace(shorthandRegex, (_m, r, g, b) => r + r + g + g + b + b)
7+
const replacedHex = hex.replace(
8+
shorthandRegex,
9+
(_m, r, g, b) => r + r + g + g + b + b,
10+
)
11+
12+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(replacedHex)
713

8-
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
914
return result
1015
? {
11-
r: parseInt(result[1], 16),
12-
g: parseInt(result[2], 16),
13-
b: parseInt(result[3], 16),
16+
r: Number.parseInt(result[1], 16),
17+
g: Number.parseInt(result[2], 16),
18+
b: Number.parseInt(result[3], 16),
1419
}
15-
: null
20+
: undefined
1621
}
22+
23+
export default hexToRgb

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { default as ColorPalette } from './ColorPalette'
21
export { default as alpha } from './alpha'
2+
export { default as ColorPalette } from './ColorPalette'
33
export { default as hexToRgb } from './hexToRgb'
44
export { default as rgbToHex } from './rgbToHex'

src/rgbToHex.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export default function rgbToHex(
2-
red: number,
3-
green: number,
4-
blue: number,
5-
): string {
1+
const rgbToHex = (red: number, green: number, blue: number): string => {
2+
// eslint-disable-next-line no-bitwise
63
const rgb = blue | (green << 8) | (red << 16)
7-
return '#' + (0x1000000 + rgb).toString(16).slice(1)
4+
5+
return `#${(0x1_00_00_00 + rgb).toString(16).slice(1)}`
86
}
7+
8+
export default rgbToHex

tsconfig.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
4-
"module": "commonjs",
3+
"allowSyntheticDefaultImports": true,
54
"declaration": true,
5+
"esModuleInterop": true,
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"noImplicitAny": true,
9+
"noUnusedLocals": true,
10+
"noUnusedParameters": true,
611
"outDir": "dist",
712
"removeComments": true,
813
"strict": true,
9-
"noImplicitAny": true,
1014
"strictNullChecks": true,
1115
"strictFunctionTypes": true,
12-
"noUnusedLocals": true,
13-
"noUnusedParameters": true,
14-
"moduleResolution": "node",
15-
"allowSyntheticDefaultImports": true,
16-
"esModuleInterop": true
16+
"target": "es2021"
1717
},
1818
"include": ["src"],
1919
"exclude": ["node_modules", "lib"]

0 commit comments

Comments
 (0)