Giter Club home page Giter Club logo

color-range's Introduction

Color Range

version size downloads license

Description

Color Range is a TypeScript library allows you to generate a percentage of mix between two colors within given a range. The provided range will determine the available midpoints, with each midpoint being a percentage of mix. It uses a linear interpolation algorithm to blend the two colors together, resulting in a smooth transition between the two. The library provides a simple and easy-to-use API, allowing you to specify the colors and the range for the blend. It can be useful for creating color schemes, gradient backgrounds, and other design elements in web and graphic design projects. It also support different color format like RGB, RGBA, and HEX. With this library, you can easily create dynamic and visually striking designs with minimal code.

Getting Started

Dependencies

Installing

  • npm i @heyeso/color-range

Usage

colorRange()

import { colorRange } from "@heyeso/color-range";

/**
 * Default colors for mapping.
 */
export const test_colors = [
    [255, 36, 0],
    [255, 0, 0],
  ];
/**
 * Default ranges for mapping.
 */
export const test_ranges = [0, 100];

const temperatureMap = colorRange(test_colors, test_ranges);
const temp1 = temperatureMap.getColor(50);

temp1?.rgb; // { r: 255, g: 18, b: 0 }
temp1?.toString; // "rgb(255, 18, 0)"
temp1?.toHex; // "#ff1200"

createColor()

import { createColor } from "@heyeso/color-range";

const color = createColor([3, 40, 69]);
color.toString; // "rgb(3, 40, 69)"
color.toHex; // "#032845"
  • isRGB()
  • isRGBString()
  • isHex()
  • stringToRGB()
  • hexToRGB()
  • arrayToRGB()

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Authors

Abdulsalam Odetayo @linkedIn

License

Distributed under the MIT License. See LICENSE for more information.

References

color-range's People

Contributors

heyeso avatar

Stargazers

 avatar

Watchers

 avatar

color-range's Issues

Refactor: refactor `Color` type.

Problem
The Color type currently returns

export type Color = {
  rgb: RGB;
  toString: () => string;
  toHex: () => string;
};

This is inefficient as the toString and toHex functions' return value persists. Therefore the function has to be run to get their respective returns, although they will always return the same value.

Feature Solution
Reimplement to have this type:

export type Color = {
  rgb: RGB;
  rgbString: string;
  Hex: string;
};

Feat: Extend `createColor` function to accept RGB values and Hex values as string

problem
Currently, the createColor function only accepts RGB values as integers. However, it would be useful to also be able to pass RGB values and Hex values as strings. This would allow for more flexibility in input and make the function more user-friendly.

Feature solution
The implementation of this feature should include validation checks to ensure that the input is in the correct format (RGB values should be in the format "rgb(x, y, z)" and hex values should be in the format "#xxxxxx"). If the input is invalid, the function should return null.

const isRGB = (value: string): value is RGB => {
   // check if the string matches the RGB object format
   ...
   // returns a boolean
}

const isRGBString = (value: string): value is RGBString => {
   // check if the string matches RGB string format
   ...
   // returns a boolean
}

const isHex = (value: string): value is Hex => {
   // check if the string matches Hex string format
   ...
   // returns a boolean
}

Migrate from Javascript to Typescript

Problem
Migrate our codebase from JavaScript to TypeScript. As part of this process, we need to update the "Temperature Color" library to use TypeScript. This library displays color on the screen based on the temperature input.

Solution

  • Update the temperature color library to use TypeScript.
  • Ensure that the library still works correctly and displays the correct color for the temperature input.
  • Update the documentation to reflect the changes made to the feature.

Color sorting inversed for first and last item in ColorRange.getColor()

Hey, nice little library i am finding useful.

I have been running into one issue though when using the tool.

So, say i want 14 different shades of blue ordered from light to dark.
I have used code similar to the below to create a map of 14 colors.

However, when querying the colorRange(test_colors, test_ranges).getColor(i).toHex method, all items are returned in the expected light to dark order, aside from the first and last item.

let startRange = 1;
let endRange = 14;

const test_colors = [
      [204, 255, 255],
      [0, 51, 51],
    ];

const test_ranges = [startRange, endRange];

for (let i = startRange; i <= endRange; i++) {
    console.log(colorRange(test_colors, test_ranges).getColor(i).toHex);
}

This image illustrates the order in which the colors are returned.
image

After a bit more testing, i found that in the color-range.ts file (line 89), if i reverse the two return statements on the two if's like below:

// if (num <= this.map.ranges[0]) return this.map.colors[0];
// if (num >= this.map.ranges[this.map.ranges.length - 1])
    //   return this.map.colors[this.map.ranges.length - 1];

if (num <= this.map.ranges[0]) return this.map.colors[this.map.ranges.length - 1];
if (num >= this.map.ranges[this.map.ranges.length - 1]) return this.map.colors[0];

Then all 14 colors are returned in the expected order including the first and last item.
This also occurs when the order of the base colors in "test_colors" are also revered from Light to Dark, to, Dark to Light

Prepare library for publish

The library is ready to be published, but it is missing some dependencies that need to be installed. These dependencies are necessary for the library to function properly and without them, users will not be able to use the library as intended.

Feat: Extend `createColor` to accept an alpha value and `Color` to accept RGBA value

Problem

  • The createColor function currently does not accept an alpha value.
  • The Color type currently returns:
    export type Color = {
      rgb: RGB;
      ...
    };

Feature Solution

  • Reimplement createColor as:
      interface CreateColor = (..., alpha?: number) => Color
  • Reimplement Color as:
      export type Color = {
        rgb: RGB;
        rgba?: RGBA;
        ...
      };
  • Implement RGBA as:
      export type RGBA = RGB & {
        a: number
      };

Prereq

Feat: handle edge cases for `createColor` function

Problem
Users may input out-of-range RGB values or strings that don't match RGB syntax or hex syntax

Feature solution
The feature solution for this problem could involve implementing validation checks for the user input to ensure that only valid RGB values or strings in the correct syntax (either RGB or hex) are accepted. This can be done by using regular expressions to match the input against the correct pattern, and displaying an error message if the input is invalid. Additionally, the feature could include a mechanism to automatically correct out-of-range RGB values.

Prereq

Import Issue

Error: Cannot find module '~/node_modules/@heyeso/color-map/dist/cjs/index.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (node:internal/modules/cjs/loader:364:19)
    at Function.Module._findPath (node:internal/modules/cjs/loader:577:18)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (~/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (~/src/index.ts:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Module.m._compile (~/node_modules/ts-node/src/index.ts:1618:23) {
  code: 'MODULE_NOT_FOUND',
  path: '~/node_modules/@heyeso/color-map/package.json',
  requestPath: '@heyeso/color-map'
}
[nodemon] app crashed - waiting for file changes before starting...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.