Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

Commit

Permalink
Add Color Luma Validations (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 committed May 17, 2021
1 parent 58f403d commit bd0fb2b
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 23 deletions.
59 changes: 46 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ All the details about contributing to the project are [described here](https://g
| `isEmoji` | `isEmoji("😂")` |
| `hasEmojis` | `hasEmojis("Yes, there is emojis here 😂")` |
| `isHerokuApiKey` | `isHerokuApiKey("625628d3-8a45-466e-a55e-ead5c6886887")` |
| `isHexColor` | `isHexColor("#000")` |
| `hasHtmlTags` | `hasHtmlTags("Foo <b>Bar</b>")` |
| `isIpv4` | `isIpv4("192.168.1.1")` |
| `isIpv4WithMask` | `isIpv4WithMask("192.168.1.1/24")` |
Expand All @@ -87,6 +86,14 @@ All the details about contributing to the project are [described here](https://g
| `isSimpleUsername` | `isSimpleUsername("foo-bar")` |
| `isUUIDv4` | `isUUIDv4("24bd85a1-4eb7-4f63-829e-75c08ac2b6c0")` |

### Colors

| Method | How To Use |
| ----------------- | ---------------------------- |
| `isDarkHexColor` | `isDarkHexColor("#000000")` |
| `isHexColor` | `isHexColor("#000")` |
| `isLightHexColor` | `isLightHexColor("#ffffff")` |

### Numbers

| Method | How To Use |
Expand Down Expand Up @@ -185,18 +192,6 @@ if (isHerokuApiKey("625628d3-8a45-466e-a55e-ead5c6886887")) {
}
```

#### isHexColor

Check if the string is a color in hex format.

```ts
import { isHexColor } from "@techmmunity/easy-check";

if (isHexColor("#000") || isHexColor("#000000")) {
// ...
}
```

#### hasHtmlTags

Check if the string contains html tags like.
Expand Down Expand Up @@ -320,6 +315,44 @@ if (isUUIDv4("24bd85a1-4eb7-4f63-829e-75c08ac2b6c0")) {
}
```

### Colors

#### isDarkHexColor

Check if the string is a dark color in hex format.

```ts
import { isDarkHexColor } from "@techmmunity/easy-check";

if (isDarkHexColor("#000") || isDarkHexColor("#000000")) {
// ...
}
```

#### isHexColor

Check if the string is a color in hex format.

```ts
import { isHexColor } from "@techmmunity/easy-check";

if (isHexColor("#000") || isHexColor("#000000")) {
// ...
}
```

#### isLightHexColor

Check if the string is a light color in hex format.

```ts
import { isLightHexColor } from "@techmmunity/easy-check";

if (isLightHexColor("#fff") || isLightHexColor("#ffffff")) {
// ...
}
```

### Numbers

#### isDivisibleByTen
Expand Down
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module.exports = {
clearMocks: true,
coverageThreshold: {
global: {
branches: 97.6,
functions: 97.6,
lines: 97.6,
statements: 97.6,
branches: 97.9,
functions: 97.9,
lines: 97.9,
statements: 97.9,
},
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@techmmunity/easy-check",
"version": "3.1.2",
"version": "3.2.0",
"main": "index.js",
"types": "index.d.ts",
"license": "Apache-2.0",
Expand Down
37 changes: 37 additions & 0 deletions src/checks/color/helpers/get-color-luma.ts
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;
};
19 changes: 19 additions & 0 deletions src/checks/color/is-dark-hex-color.ts
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.
19 changes: 19 additions & 0 deletions src/checks/color/is-light-hex-color.ts
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;
},
});
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export * from "./checks/color/is-dark-hex-color";
export * from "./checks/color/is-hex-color";
export * from "./checks/color/is-light-hex-color";

export * from "./checks/cpf/is-cpf";
export * from "./checks/cpf/is-masked-cpf";

Expand All @@ -16,8 +20,6 @@ export * from "./checks/emoji/is-emoji";

export * from "./checks/heroku/is-heroku-api-key";

export * from "./checks/hex/is-hex-color";

export * from "./checks/html/has-html-tags";

export * from "./checks/ip/is-ipv4";
Expand Down
74 changes: 74 additions & 0 deletions src/tests/color/is-dark-hex-color.spec.ts
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);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isHexColor } from "checks/hex/is-hex-color";
import { isHexColor } from "checks/color/is-hex-color";

/**
*
Expand Down Expand Up @@ -61,7 +61,7 @@ describe("isHexColor (return False)", () => {
it("length < 3", () => {
expect(isHexColor("#7b")).toBe(false);
expect(isHexColor("#eb")).toBe(false);
expect(isHexColor("#55")).toBe(false);
expect(isHexColor("#9a")).toBe(false);
expect(isHexColor("#5")).toBe(false);
expect(isHexColor("#9")).toBe(false);
});
});
74 changes: 74 additions & 0 deletions src/tests/color/is-light-hex-color.spec.ts
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);
});
});

0 comments on commit bd0fb2b

Please sign in to comment.