Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(v5.0.0): hex to grb returns undefined instead of null #21

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
language: "en-US"
reviews:
poem: false
review_status: false
auto_review:
enabled: true
drafts: false
7 changes: 1 addition & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier"
],
"plugins": ["@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"extends": ["@allthings/eslint-config"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off"
}
Expand Down
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This PR implements ...
80 changes: 80 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: pipeline
on: [ push ]

env:
TAG_NAME: $(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9._-]/_/g')
NVM_NODE_VERSION: 18
PROJECT: colors

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build-and-lint:
runs-on:
group: default-runners
labels: self-hosted
steps:
- uses: actions/checkout@v4
env:
CURRENT_RUNNER: ${{ runner.name }}

- uses: actions/setup-node@v4
with:
node-version: ${{ env.NVM_NODE_VERSION }}

- name: build-and-lint
shell: bash
run: |
./bin/node-version-manager.sh yarn install --production=false --frozen-lockfile
./bin/node-version-manager.sh yarn build
./bin/node-version-manager.sh yarn lint

dry-run-publish:
needs: [ build-and-lint ]
runs-on:
group: default-runners
labels: self-hosted
steps:
- uses: actions/checkout@v4
env:
CURRENT_RUNNER: ${{ runner.name }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

- uses: actions/setup-node@v4
with:
node-version: ${{ env.NVM_NODE_VERSION }}

- name: dry-run-publish
shell: bash
run: |
./bin/node-version-manager.sh yarn install --production=false --frozen-lockfile
./bin/node-version-manager.sh yarn build
./bin/node-version-manager.sh npm publish --access public --tag latest --dry-run

publish-npm:
needs: [ dry-run-publish ]
# TODO: not working yet
if: false
# if: github.ref == 'refs/heads/master'
runs-on:
group: default-runners
labels: self-hosted
steps:
- uses: actions/checkout@v4
env:
CURRENT_RUNNER: ${{ runner.name }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NVM_NODE_VERSION }}

- name: publish-npm
shell: bash
run: |
./bin/node-version-manager.sh yarn install --production=false --frozen-lockfile
./bin/node-version-manager.sh yarn build
./bin/node-version-manager.sh npm publish --access public --tag latest
19 changes: 19 additions & 0 deletions bin/node-version-manager.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# shellcheck disable=SC1090
source ~/.bashrc
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
source ~/.nvm/bash_completion
{
nvm install "v$NVM_NODE_VERSION" --lts
nvm alias default "v$NVM_NODE_VERSION"
nvm alias node "v$NVM_NODE_VERSION"
nvm alias system "v$NVM_NODE_VERSION"
nvm use "v$NVM_NODE_VERSION"
npm install -g yarn

"$@"
} || {
echo "An error occurred during execution."
exit 1
}
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
"name": "@allthings/colors",
"author": "Allthings",
"license": "MIT",
"version": "4.0.2",
"version": "5.0.0",
"files": [
"dist"
],
"main": "dist/index.js",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"eslint": "^8.38.0",
"eslint-config-prettier": "^8.8.0",
"prettier": "^2.8.7",
"rimraf": "^5.0.0",
"typescript": "^5.0.4"
"@allthings/eslint-config": "^2.1.1",
"eslint": "^8.57.0",
"prettier": "^3.3.2",
"rimraf": "^5.0.7",
"typescript": "^5.5.2"
},
"prettier": {
"semi": false,
Expand Down
3 changes: 2 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": ["@allthings", ":preserveSemverRanges"]
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["github>allthings/renovate-config", "docker:disable"]
}
5 changes: 5 additions & 0 deletions src/ColorPalette.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable sort-keys */
const palette = {
lightBlack: '#2c2c2c',
lightBlackIntense: '#232323',
Expand Down Expand Up @@ -49,12 +50,14 @@ const background = {
white: palette.white,
none: palette.none,
}

const text = {
primary: '#333333',
secondary: '#626262',
gray: '#bbbbbb',
lightGray: '#e7ecee',
}

const primary = {
gray: palette.lightGreyIntense,
blue: palette.blue,
Expand All @@ -63,11 +66,13 @@ const primary = {
red: palette.brownIntense,
orange: palette.yellowOrangeIntense,
}

const state = {
success: palette.mintIntense,
warning: palette.yellowOrangeIntense,
error: palette.redIntense,
}

const social = {
facebook: '#3b5998',
google: '#c53929',
Expand Down
25 changes: 14 additions & 11 deletions src/alpha.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import hexToRgb from './hexToRgb'
import rgbToHex from './rgbToHex'

export interface RGBColor {
/* eslint-disable sort-keys, typescript-sort-keys/interface */
export interface IRGBColor {
r: number
g: number
b: number
}

function calculateTransparentColor(color: RGBColor, alpha: number) {
const calculateTransparentColor = (color: IRGBColor, alphaNumber: number) => {
const blendingColor = {
r: 255,
g: 255,
b: 255,
}

return rgbToHex(
(1 - alpha) * blendingColor.r + alpha * color.r,
(1 - alpha) * blendingColor.g + alpha * color.g,
(1 - alpha) * blendingColor.b + alpha * color.b,
(1 - alphaNumber) * blendingColor.r + alphaNumber * color.r,
(1 - alphaNumber) * blendingColor.g + alphaNumber * color.g,
(1 - alphaNumber) * blendingColor.b + alphaNumber * color.b,
)
}

export default function alpha(
const alpha = (
hex: string,
alpha: number,
alphaNumber: number,
actualColor?: boolean,
): string {
): string => {
const rgb = hexToRgb(hex)
if (rgb === null) {
if (rgb === undefined) {
throw new Error(
`\`hex\` value provided to \`alpha\` is not a HEX color, \`${hex}\` given.`,
)
}

return actualColor
? calculateTransparentColor(rgb, alpha)
: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})`
? calculateTransparentColor(rgb, alphaNumber)
: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alphaNumber})`
}

export default alpha
25 changes: 16 additions & 9 deletions src/hexToRgb.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { RGBColor } from './alpha'

export default function hexToRgb(hex: string): RGBColor | null {
/* eslint-disable sort-keys */
const hexToRgb = (
hex: string,
): { b: number; g: number; r: number } | undefined => {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
hex = hex.replace(shorthandRegex, (_m, r, g, b) => r + r + g + g + b + b)
const replacedHex = hex.replace(
shorthandRegex,
(_m, r, g, b) => r + r + g + g + b + b,
)

const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(replacedHex)

const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
r: Number.parseInt(result[1], 16),
g: Number.parseInt(result[2], 16),
b: Number.parseInt(result[3], 16),
}
: null
: undefined
}

export default hexToRgb
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as ColorPalette } from './ColorPalette'
export { default as alpha } from './alpha'
export { default as ColorPalette } from './ColorPalette'
export { default as hexToRgb } from './hexToRgb'
export { default as rgbToHex } from './rgbToHex'
12 changes: 6 additions & 6 deletions src/rgbToHex.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function rgbToHex(
red: number,
green: number,
blue: number,
): string {
const rgbToHex = (red: number, green: number, blue: number): string => {
// eslint-disable-next-line no-bitwise
const rgb = blue | (green << 8) | (red << 16)
return '#' + (0x1000000 + rgb).toString(16).slice(1)

return `#${(0x1_00_00_00 + rgb).toString(16).slice(1)}`
}

export default rgbToHex
16 changes: 8 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"removeComments": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
"target": "es2021"
},
"include": ["src"],
"exclude": ["node_modules", "lib"]
Expand Down
Loading