This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58f403d
commit bd0fb2b
Showing
11 changed files
with
281 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const getColorWithCorrectLength = (color: string) => { | ||
if (color.length === 3) { | ||
const colorSplitted = color.split(""); | ||
|
||
/** | ||
* Duplicate the values, to make the code have length of six | ||
*/ | ||
const colorWithLengthSix = [ | ||
colorSplitted[0], | ||
colorSplitted[0], | ||
colorSplitted[1], | ||
colorSplitted[1], | ||
colorSplitted[2], | ||
colorSplitted[2], | ||
]; | ||
|
||
return colorWithLengthSix.join(""); | ||
} | ||
|
||
return color; | ||
}; | ||
|
||
export const getColorLuma = (color: string) => { | ||
const colorWithOutHashtag = color.substring(1); | ||
|
||
const colorWithLengthSix = getColorWithCorrectLength(colorWithOutHashtag); | ||
|
||
const rgb = parseInt(colorWithLengthSix, 16); | ||
|
||
const r = (rgb >> 16) & 0xff; | ||
const g = (rgb >> 8) & 0xff; | ||
const b = (rgb >> 0) & 0xff; | ||
|
||
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
|
||
return luma; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { makeFunction } from "helpers/make-function"; | ||
|
||
import { getColorLuma } from "./helpers/get-color-luma"; | ||
import { isHexColor } from "./is-hex-color"; | ||
|
||
/** | ||
* Check if a string is a dark hex color | ||
*/ | ||
export const isDarkHexColor = makeFunction<string>({ | ||
func: (color: string) => { | ||
if (isHexColor(color)) { | ||
const luma = getColorLuma(color); | ||
|
||
return luma < 50; | ||
} | ||
|
||
return false; | ||
}, | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { makeFunction } from "helpers/make-function"; | ||
|
||
import { getColorLuma } from "./helpers/get-color-luma"; | ||
import { isHexColor } from "./is-hex-color"; | ||
|
||
/** | ||
* Check if a string is a light hex color | ||
*/ | ||
export const isLightHexColor = makeFunction<string>({ | ||
func: (color: string) => { | ||
if (isHexColor(color)) { | ||
const luma = getColorLuma(color); | ||
|
||
return luma > 50; | ||
} | ||
|
||
return false; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { isDarkHexColor } from "checks/color/is-dark-hex-color"; | ||
|
||
/** | ||
* | ||
* True | ||
* | ||
*/ | ||
|
||
describe("isDarkHexColor (return True)", () => { | ||
it("with valid hex colors", () => { | ||
expect(isDarkHexColor("#000")).toBe(true); | ||
expect(isDarkHexColor("#000000")).toBe(true); | ||
expect(isDarkHexColor("#8b0000")).toBe(true); | ||
expect(isDarkHexColor("#381022")).toBe(true); | ||
expect(isDarkHexColor("#24110a")).toBe(true); | ||
expect(isDarkHexColor("#06040e")).toBe(true); | ||
expect(isDarkHexColor("#4c1551")).toBe(true); | ||
expect(isDarkHexColor("#4a1515")).toBe(true); | ||
expect(isDarkHexColor("#21195f")).toBe(true); | ||
expect(isDarkHexColor("#361148")).toBe(true); | ||
expect(isDarkHexColor("#21391e")).toBe(true); | ||
expect(isDarkHexColor("#140d07")).toBe(true); | ||
}); | ||
}); | ||
|
||
/** | ||
* | ||
* False | ||
* | ||
*/ | ||
|
||
describe("isDarkHexColor (return False)", () => { | ||
it("with light colors", () => { | ||
expect(isDarkHexColor("#fff")).toBe(false); | ||
expect(isDarkHexColor("#ffffff")).toBe(false); | ||
expect(isDarkHexColor("#add8e6")).toBe(false); | ||
expect(isDarkHexColor("#90ee90")).toBe(false); | ||
expect(isDarkHexColor("#FF9999")).toBe(false); | ||
expect(isDarkHexColor("#F5554D")).toBe(false); | ||
expect(isDarkHexColor("#FF3D0D")).toBe(false); | ||
expect(isDarkHexColor("#FCAF94")).toBe(false); | ||
expect(isDarkHexColor("#FB861A")).toBe(false); | ||
expect(isDarkHexColor("#FCB514")).toBe(false); | ||
expect(isDarkHexColor("#D5D2C1")).toBe(false); | ||
expect(isDarkHexColor("#B8B85A")).toBe(false); | ||
expect(isDarkHexColor("#B1DD27")).toBe(false); | ||
}); | ||
|
||
it("without #", () => { | ||
expect(isDarkHexColor("000")).toBe(false); | ||
expect(isDarkHexColor("000000")).toBe(false); | ||
}); | ||
|
||
it("length > 6", () => { | ||
expect(isDarkHexColor("#8b00000")).toBe(false); | ||
expect(isDarkHexColor("#3810222")).toBe(false); | ||
expect(isDarkHexColor("#24110aa")).toBe(false); | ||
expect(isDarkHexColor("#54a1515")).toBe(false); | ||
}); | ||
|
||
it("3 < length > 6", () => { | ||
expect(isDarkHexColor("#8b000")).toBe(false); | ||
expect(isDarkHexColor("#3815")).toBe(false); | ||
expect(isDarkHexColor("#3845")).toBe(false); | ||
expect(isDarkHexColor("#97699")).toBe(false); | ||
}); | ||
|
||
it("length < 3", () => { | ||
expect(isDarkHexColor("#3f")).toBe(false); | ||
expect(isDarkHexColor("#21")).toBe(false); | ||
expect(isDarkHexColor("#2")).toBe(false); | ||
expect(isDarkHexColor("#3")).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { isLightHexColor } from "checks/color/is-light-hex-color"; | ||
|
||
/** | ||
* | ||
* True | ||
* | ||
*/ | ||
|
||
describe("isLightHexColor (return True)", () => { | ||
it("with valid hex colors", () => { | ||
expect(isLightHexColor("#fff")).toBe(true); | ||
expect(isLightHexColor("#ffffff")).toBe(true); | ||
expect(isLightHexColor("#add8e6")).toBe(true); | ||
expect(isLightHexColor("#90ee90")).toBe(true); | ||
expect(isLightHexColor("#FF9999")).toBe(true); | ||
expect(isLightHexColor("#F5554D")).toBe(true); | ||
expect(isLightHexColor("#FF3D0D")).toBe(true); | ||
expect(isLightHexColor("#FCAF94")).toBe(true); | ||
expect(isLightHexColor("#FB861A")).toBe(true); | ||
expect(isLightHexColor("#FCB514")).toBe(true); | ||
expect(isLightHexColor("#D5D2C1")).toBe(true); | ||
expect(isLightHexColor("#B8B85A")).toBe(true); | ||
expect(isLightHexColor("#B1DD27")).toBe(true); | ||
}); | ||
}); | ||
|
||
/** | ||
* | ||
* False | ||
* | ||
*/ | ||
|
||
describe("isLightHexColor (return False)", () => { | ||
it("with dark colors", () => { | ||
expect(isLightHexColor("#000")).toBe(false); | ||
expect(isLightHexColor("#000000")).toBe(false); | ||
expect(isLightHexColor("#8b0000")).toBe(false); | ||
expect(isLightHexColor("#381022")).toBe(false); | ||
expect(isLightHexColor("#24110a")).toBe(false); | ||
expect(isLightHexColor("#06040e")).toBe(false); | ||
expect(isLightHexColor("#4c1551")).toBe(false); | ||
expect(isLightHexColor("#4a1515")).toBe(false); | ||
expect(isLightHexColor("#21195f")).toBe(false); | ||
expect(isLightHexColor("#361148")).toBe(false); | ||
expect(isLightHexColor("#21391e")).toBe(false); | ||
expect(isLightHexColor("#140d07")).toBe(false); | ||
}); | ||
|
||
it("without #", () => { | ||
expect(isLightHexColor("fff")).toBe(false); | ||
expect(isLightHexColor("ffffff")).toBe(false); | ||
}); | ||
|
||
it("length > 6", () => { | ||
expect(isLightHexColor("#90ee900")).toBe(false); | ||
expect(isLightHexColor("#FF99999")).toBe(false); | ||
expect(isLightHexColor("#F5554DD")).toBe(false); | ||
expect(isLightHexColor("#FF3D0DD")).toBe(false); | ||
}); | ||
|
||
it("3 < length > 6", () => { | ||
expect(isLightHexColor("#90ee9")).toBe(false); | ||
expect(isLightHexColor("#FF99")).toBe(false); | ||
expect(isLightHexColor("#F555")).toBe(false); | ||
expect(isLightHexColor("#FF3D0")).toBe(false); | ||
}); | ||
|
||
it("length < 3", () => { | ||
expect(isLightHexColor("#90")).toBe(false); | ||
expect(isLightHexColor("#FF")).toBe(false); | ||
expect(isLightHexColor("#F")).toBe(false); | ||
expect(isLightHexColor("#5")).toBe(false); | ||
}); | ||
}); |