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

Ported & merged RGB & HSL modules #1

Merged
merged 1 commit into from
Mar 14, 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
33 changes: 33 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

################################################################################
# #
# Editorconfig #
# #
# This file defines basic file properties that #
# editors / IDEs use when you modify documents. #
# #
# Check https://EditorConfig.org for details. #
# #
################################################################################


root = true


[*]

end_of_line = lf


[*.{editorconfig,json,yml,ts,md}]

trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
charset = utf-8


[*.md]

trim_trailing_whitespace = false
27 changes: 27 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

name : Publish


on :
workflow_dispatch :
push :
branches : [ Stable ]


jobs :
publish :

runs-on : ubuntu-latest

permissions :

id-token : write
contents : read

steps :

- name : Checkout Repository
uses : actions/checkout@v4

- name : Publish To JSR
run : npx jsr publish
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

name : Test


on :
workflow_dispatch :
pull_request :
branches : [ Stable ]


jobs :
publish :

runs-on : ubuntu-latest

strategy :
matrix :
deno-version : [ v1.41.x ]

steps :

- name : Checkout Repository
uses : actions/checkout@v4

- name : Setup Deno
uses : denoland/setup-deno@v1
with :
deno-version : ${{ matrix.deno-version }}

- name : Run Tests
run : deno test
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations" : [
"JeremyFunk.hidefiles"
]
}
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

<br>

<div align = center>

# Omega / Color

*Various color conversions.*

<br>

[![Badge Stars]][Stars]   

</div>

<br>

### Hex RGB(A)

```typescript
const hex = '#0000FF' // Blue

const rgb = hexToRGB(hex)

console.debug(rgb) // [ 0 , 0 , 255 ]
```

<br>

### HSL(A)

```typescript
const hsl = [ 0 , 100 , 50 ] // Red

const rgb = hslToRGB(hsl)

console.debug(rgb) // [ 255 , 0 , 0 ]
```

<br>

### CMYK(A)

```typescript
const cmyka = [ 100 , 0 , 100 , 0 ] // Green

const rgb = cmykToRGB(cmyka)

console.debug(rgb) // [ 120 , 100 , 50 ]
```

<br>


<!----------------------------------------------------------------------------->

[Stars]: https://github.com/OmegaTools/Color
[Usage]: Documentation/Usage.md


<!---------------------------------[ Badges ]---------------------------------->

[Badge Stars]: https://img.shields.io/github/stars/OmegaTools/RGB?style=for-the-badge&logoColor=white&logo=Trustpilot&labelColor=FF66AA&color=cf538b

38 changes: 38 additions & 0 deletions Source/HSL/CMYK.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


import { assertEquals } from 'Assert'
import { cmykToHSL } from './CMYK.ts'


function assert ( a : Array<number> , b : Array<number> ){
assertEquals(cmykToHSL(a),b)
}


Deno.test('CMYK Array Conversion',() => {

// Black

assert([ 0 , 0 , 0 , 100 ],[ 0 , 0 , 0 ])


// White

assert([ 0 , 0 , 0 , 0 ],[ 0 , 0 , 100 ])


// Red

assert([ 0 , 100 , 100 , 0 ],[ 0 , 100 , 50 ])


// Green

assert([ 100 , 0 , 100 , 0 ],[ 120 , 100 , 50 ])


// Blue

assert([ 100 , 100 , 0 , 0 ],[ 240 , 100 , 50 ])

})
42 changes: 42 additions & 0 deletions Source/HSL/CMYK.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

export { cmykToHSL }

import { rgbToHSL } from './RGB.ts'


function normalize ( channels : Array<number> ){
return channels
.map(( channel ) => channel / 100 )
.map(( channel ) => 1 - channel )
}


function toHSL (
cymk : Array<number>
){

const [ C , M , Y , K ] =
normalize(cymk)

return rgbToHSL([
255 * C * K ,
255 * M * K ,
255 * Y * K ,
cymk[3]
])
}


/**
* Converts CMYK(A) color arrays to HSL(A) color arrays.
*
* @param channels [ Cyan , Magenta , Yellow , Key , ( Alpha ) ]
* @returns [ Hue , Saturation , Lightness , ( Alpha ) ]
*/

function cmykToHSL (
cmyk : Array<number>
) : Array<number> {
return toHSL(cmyk)
.concat(cmyk.slice(3,1))
}
38 changes: 38 additions & 0 deletions Source/HSL/Hex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


import { parseHexToHSL } from './Hex.ts'
import { assertEquals } from 'Assert'


function assert ( a : string , b : null | Array<number> ){
assertEquals(parseHexToHSL(a),b)
}


Deno.test('Hex String Conversion',() => {

// Black

assert('#000000',[ 0 , 0 , 0 ])


// White

assert('#FFFFFF',[ 0 , 0 , 100 ])


// Red

assert('#FF0000',[ 0 , 100 , 50 ])


// Green

assert('#00FF00',[ 120 , 100 , 50 ])


// Blue

assert('#0000FF',[ 240 , 100 , 50 ])

})
44 changes: 44 additions & 0 deletions Source/HSL/Hex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

export { parseHexToHSL , hexToHSL }


import { parseHexToRGB , hexToRGB } from '../mod.ts'
import { rgbToHSL } from './RGB.ts'


/**
* Parses hex color codes into their respective HSL(A) channels.
*
* @param hex Hex HSL(A) color code string.
* @returns Channels [ Hue , Saturation , Lightness , ( Alpha ) ] each ( 0 - 255 )
*
* Check {@link hexToRGB} for examples
*/

function hexToHSL (
hex : string
) : Array<number> {
return rgbToHSL(hexToRGB(hex))
}


/**
* Attempts to parse a string with a hex color code into its respective HSL(A) channels.
*
* @param hex String possibly containing a hex HSL(A) color code.
* @returns null if not found, otherwise Channels [ Hue , Saturation , Lightness , ( Alpha ) ] each ( 0 - 255 )
*
* Check {@link hexToRGB} for examples
*/

function parseHexToHSL (
hex : string
) : null | Array<number> {

const rgb = parseHexToRGB(hex)

if( rgb )
return rgbToHSL(rgb)

return null
}
38 changes: 38 additions & 0 deletions Source/HSL/RGB.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


import { assertEquals } from 'Assert'
import { rgbToHSL } from './RGB.ts'


function assert ( a : Array<number> , b : Array<number> ){
assertEquals(rgbToHSL(a),b)
}


Deno.test('RGB Array Conversion',() => {

// Black

assert([ 0 , 0 , 0 ],[ 0 , 0 , 0 ])


// White

assert([ 255 , 255 , 255 ],[ 0 , 0 , 100 ])


// Red

assert([ 255 , 0 , 0 ],[ 0 , 100 , 50 ])


// Green

assert([ 0 , 255 , 0 ],[ 120 , 100 , 50 ])


// Blue

assert([ 0 , 0 , 255 ],[ 240 , 100 , 50 ])

})
Loading