@jiaminghi/color
Advanced tools
Comparing version 0.0.6 to 0.1.0
@@ -0,1 +1,7 @@ | ||
# 0.1.0-alpha (2019-07-07) | ||
### Perfect | ||
- **keywords:** Support color keywords like red, blue, transparent and etc. | ||
# 0.0.6-alpha (2019-06-13) | ||
@@ -2,0 +8,0 @@ |
223
lib/index.js
@@ -7,6 +7,6 @@ "use strict"; | ||
exports.getRgbValue = getRgbValue; | ||
exports.getRgbaValue = getRgbaValue; | ||
exports.getOpacity = getOpacity; | ||
exports.toRgb = toRgb; | ||
exports.toHex = toHex; | ||
exports.getOpacity = getOpacity; | ||
exports.getRgbaValue = getRgbaValue; | ||
exports.getColorFromRgbValue = getColorFromRgbValue; | ||
@@ -18,2 +18,6 @@ exports.darken = darken; | ||
var _keywords = _interopRequireDefault(require("./config/keywords")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
@@ -31,21 +35,53 @@ | ||
/** | ||
* @description Get the rgb value of the color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Array} Rgb value of the color | ||
* @description Color validator | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {String|Boolean} Valid color Or false | ||
*/ | ||
function validator(color) { | ||
var isHex = hexReg.test(color); | ||
var isRgb = rgbReg.test(color); | ||
if (isHex || isRgb) return color; | ||
color = getColorByKeyword(color); | ||
if (!color) { | ||
console.error('Color: Invalid color!'); | ||
return false; | ||
} | ||
return color; | ||
} | ||
/** | ||
* @description Get color by keyword | ||
* @param {String} keyword Color keyword like red, green and etc. | ||
* @return {String|Boolean} Hex or rgba color (Invalid keyword will return false) | ||
*/ | ||
function getColorByKeyword(keyword) { | ||
if (!keyword) { | ||
console.error('getColorByKeywords: Missing parameters!'); | ||
return false; | ||
} | ||
if (!_keywords["default"].has(keyword)) return false; | ||
return _keywords["default"].get(keyword); | ||
} | ||
/** | ||
* @description Get the Rgb value of the color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Array<Number>|Boolean} Rgb value of the color (Invalid input will return false) | ||
*/ | ||
function getRgbValue(color) { | ||
if (!color) { | ||
console.error('GetRgbValue Missing parameters!'); | ||
return; | ||
console.error('getRgbValue: Missing parameters!'); | ||
return false; | ||
} | ||
color = validator(color); | ||
if (!color) return false; | ||
var isHex = hexReg.test(color); | ||
var isRgb = rgbReg.test(color); | ||
if (!isHex && !isRgb) { | ||
console.error('Incorrect color type!'); | ||
return; | ||
} | ||
var lowerColor = color.toLowerCase(); | ||
@@ -58,3 +94,3 @@ if (isHex) return getRgbValueFromHex(lowerColor); | ||
* @param {String} color Hex color | ||
* @return {Array} Rgb value of the color | ||
* @return {Array<Number>} Rgb value of the color | ||
*/ | ||
@@ -86,87 +122,85 @@ | ||
/** | ||
* @description Convert color to rgb/rgba color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @param {Number} opacity The opacity of color | ||
* @return {String} Rgb/rgba color | ||
* @description Get the Rgba value of the color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Array<Number>|Boolean} Rgba value of the color (Invalid input will return false) | ||
*/ | ||
function toRgb(color, opacity) { | ||
function getRgbaValue(color) { | ||
if (!color) { | ||
console.error('ToRgb Missing parameters!'); | ||
return; | ||
console.error('getRgbaValue: Missing parameters!'); | ||
return false; | ||
} | ||
var rgbValue = getRgbValue(color); | ||
var addOpacity = typeof opacity === 'number'; | ||
if (addOpacity) return 'rgba(' + rgbValue.join(',') + ",".concat(opacity, ")"); | ||
return 'rgb(' + rgbValue.join(',') + ')'; | ||
var colorValue = getRgbValue(color); | ||
if (!colorValue) return false; | ||
colorValue.push(getOpacity(color)); | ||
return colorValue; | ||
} | ||
/** | ||
* @description Convert color to hex color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {String} Hex color | ||
* @description Get the opacity of color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Number|Boolean} Color opacity (Invalid input will return false) | ||
*/ | ||
function toHex(color) { | ||
function getOpacity(color) { | ||
if (!color) { | ||
console.error('ToHex Missing parameters!'); | ||
return; | ||
console.error('getOpacity: Missing parameters!'); | ||
return false; | ||
} | ||
color = validator(color); | ||
if (!color) return false; | ||
var isRgba = rgbaReg.test(color); | ||
if (!isRgba) return 1; | ||
color = color.toLowerCase(); | ||
if (hexReg.test(color)) return color; | ||
if (!rgbReg.test(color)) { | ||
console.warn('Incorrect color type!'); | ||
return; | ||
} | ||
color = getRgbValue(color); | ||
return '#' + color.map(function (n) { | ||
return Number(n).toString(16); | ||
}).map(function (n) { | ||
return n === '0' ? '00' : n; | ||
}).join(''); | ||
return Number(color.split(',').slice(-1)[0].replace(/[)|\s]/g, '')); | ||
} | ||
/** | ||
* @description Get the opacity of color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Number} Color opacity | ||
* @description Convert color to Rgb|Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @param {Number} opacity The opacity of color | ||
* @return {String|Boolean} Rgb|Rgba color (Invalid input will return false) | ||
*/ | ||
function getOpacity(color) { | ||
function toRgb(color, opacity) { | ||
if (!color) { | ||
console.error('GetOpacity Missing parameters!'); | ||
return; | ||
console.error('toRgb: Missing parameters!'); | ||
return false; | ||
} | ||
var isRgba = rgbaReg.test(color); | ||
color = color.toLowerCase(); | ||
if (!isRgba) return 1; | ||
return Number(color.split(',').slice(-1)[0].replace(/[)|\s]/g, '')); | ||
var rgbValue = getRgbValue(color); | ||
if (!rgbValue) return false; | ||
var addOpacity = typeof opacity === 'number'; | ||
if (addOpacity) return 'rgba(' + rgbValue.join(',') + ",".concat(opacity, ")"); | ||
return 'rgb(' + rgbValue.join(',') + ')'; | ||
} | ||
/** | ||
* @description Get the rgba value of the color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Array} Rgba value of the color | ||
* @description Convert color to Hex color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {String|Boolean} Hex color (Invalid input will return false) | ||
*/ | ||
function getRgbaValue(color) { | ||
function toHex(color) { | ||
if (!color) { | ||
console.error('GetRgbaValue Missing parameters!'); | ||
return; | ||
console.error('toHex: Missing parameters!'); | ||
return false; | ||
} | ||
var colorValue = getRgbValue(color); | ||
colorValue.push(getOpacity(color)); | ||
return colorValue; | ||
if (hexReg.test(color)) return color; | ||
color = getRgbValue(color); | ||
if (!color) return false; | ||
return '#' + color.map(function (n) { | ||
return Number(n).toString(16); | ||
}).map(function (n) { | ||
return n === '0' ? '00' : n; | ||
}).join(''); | ||
} | ||
/** | ||
* @description Get Color from rgb value | ||
* @param {Array} value Rgb color value | ||
* @return {String} Rgb / rgba color | ||
* @description Get Color from Rgb|Rgba value | ||
* @param {Array<Number>} value Rgb|Rgba color value | ||
* @return {String|Boolean} Rgb|Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -177,4 +211,4 @@ | ||
if (!value) { | ||
console.error('GetColorFromRgbValue Missing parameters!'); | ||
return; | ||
console.error('getColorFromRgbValue: Missing parameters!'); | ||
return false; | ||
} | ||
@@ -185,4 +219,4 @@ | ||
if (valueLength !== 3 && valueLength !== 4) { | ||
console.error('value is illegal!'); | ||
return; | ||
console.error('getColorFromRgbValue: Value is illegal!'); | ||
return false; | ||
} | ||
@@ -196,18 +230,20 @@ | ||
* @description Deepen color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Number} Percent of Deepen | ||
* @return {String} Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Number} Percent of Deepen (1-100) | ||
* @return {String|Boolean} Rgba color (Invalid input will return false) | ||
*/ | ||
function darken(color, percent) { | ||
function darken(color) { | ||
var percent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
if (!color) { | ||
console.error('Darken Missing parameters!'); | ||
return; | ||
console.error('darken: Missing parameters!'); | ||
return false; | ||
} | ||
var rgbaValue = getRgbaValue(color); | ||
if (!rgbaValue) return; | ||
if (!rgbaValue) return false; | ||
rgbaValue = rgbaValue.map(function (v, i) { | ||
return i === 3 ? v : v - 25 * percent; | ||
return i === 3 ? v : v - Math.ceil(2.55 * percent); | ||
}).map(function (v) { | ||
@@ -220,18 +256,20 @@ return v < 0 ? 0 : v; | ||
* @description Brighten color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Number} Percent of brighten | ||
* @return {String} Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Number} Percent of brighten (1-100) | ||
* @return {String|Boolean} Rgba color (Invalid input will return false) | ||
*/ | ||
function lighten(color, percent) { | ||
function lighten(color) { | ||
var percent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
if (!color) { | ||
console.error('Lighten Missing parameters!'); | ||
return; | ||
console.error('lighten: Missing parameters!'); | ||
return false; | ||
} | ||
var rgbaValue = getRgbaValue(color); | ||
if (!rgbaValue) return; | ||
if (!rgbaValue) return false; | ||
rgbaValue = rgbaValue.map(function (v, i) { | ||
return i === 3 ? v : v + 25 * percent; | ||
return i === 3 ? v : v + Math.ceil(2.55 * percent); | ||
}).map(function (v) { | ||
@@ -244,5 +282,5 @@ return v > 255 ? 255 : v; | ||
* @description Adjust color opacity | ||
* @param {String} color Hex or rgb/rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @param {Number} Percent of opacity | ||
* @return {String} Rgba color | ||
* @return {String|Boolean} Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -255,7 +293,8 @@ | ||
if (!color) { | ||
console.error('Fade Missing parameters!'); | ||
return; | ||
console.error('fade: Missing parameters!'); | ||
return false; | ||
} | ||
var rgbValue = getRgbValue(color); | ||
if (!rgbValue) return false; | ||
var rgbaValue = [].concat(_toConsumableArray(rgbValue), [percent / 100]); | ||
@@ -266,7 +305,7 @@ return getColorFromRgbValue(rgbaValue); | ||
var _default = { | ||
darken: darken, | ||
lighten: lighten, | ||
fade: fade, | ||
toHex: toHex, | ||
toRgb: toRgb, | ||
darken: darken, | ||
lighten: lighten, | ||
getOpacity: getOpacity, | ||
@@ -273,0 +312,0 @@ getRgbValue: getRgbValue, |
{ | ||
"name": "@jiaminghi/color", | ||
"version": "0.0.6", | ||
"version": "0.1.0", | ||
"author": "JiaMing <743192023@qq.com>", | ||
@@ -14,3 +14,3 @@ "description": "Color extension", | ||
"prepublish": "npm run compile", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha" | ||
}, | ||
@@ -33,7 +33,12 @@ "license": "MIT", | ||
"@babel/core": "^7.4.5", | ||
"@babel/preset-env": "^7.4.5" | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/register": "^7.4.4", | ||
"babel-register": "^6.26.0", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.1.4" | ||
}, | ||
"dependencies": { | ||
"@babel/polyfill": "^7.4.4" | ||
"@babel/polyfill": "^7.4.4", | ||
"core-js": "^3.1.4" | ||
} | ||
} |
@@ -0,4 +1,7 @@ | ||
[中文](./README_CN.md) | ||
<h1 align="center">Color Extension</h1> | ||
<p align="center"> | ||
<a href="https://travis-ci.com/jiaming743/Color"><img src="https://img.shields.io/travis/com/jiaming743/color.svg" alt="Travis CI"></a> | ||
<a href="https://github.com/jiaming743/Color/blob/master/LICENSE"><img src="https://img.shields.io/github/license/jiaming743/bezierCurve.svg" alt="LICENSE" /> </a> | ||
@@ -23,7 +26,7 @@ <a href="https://www.npmjs.com/package/@jiaminghi/color"><img src="https://img.shields.io/npm/v/@jiaminghi/color.svg" alt="NPM" /> </a> | ||
Convert color to hex color. | ||
Convert color to Hex color. | ||
- **[toRgb](#toRgb)** | ||
Convert color to rgb/rgba color. | ||
Convert color to Rgb|Rgba color. | ||
@@ -36,12 +39,16 @@ - **[getOpacity](#getOpacity)** | ||
Get the color rgb value. | ||
Get the color Rgb value. | ||
- **[getRgbaValue](#getRgbaValue)** | ||
Get the color rgba value. | ||
Get the color Rgba value. | ||
- **[getColorFromRgbValue](#getColorFromRgbValue)** | ||
Get Color from rgb value. | ||
Get Color from Rgb|Rgba value. | ||
- **[keywords](#keywords)** | ||
Built-in color keywords. | ||
### Install with npm | ||
@@ -53,2 +60,10 @@ | ||
### use | ||
```javascript | ||
import color from '@jiaminghi/color' | ||
color.toHex('black') | ||
``` | ||
------ | ||
@@ -63,5 +78,5 @@ | ||
* @description Deepen color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Number} Percent of Deepen | ||
* @return {String} Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @param {Number} percent Percent of Deepen (1-100) | ||
* @return {String|Boolean} Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -75,3 +90,3 @@ function darken (color, percent) { | ||
const after = darken(color, 20) | ||
// after = 'rgba(0,78,182,1)' | ||
// after = 'rgba(0,77,181,1)' | ||
``` | ||
@@ -90,5 +105,5 @@ | ||
* @description Brighten color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Number} Percent of brighten | ||
* @return {String} Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @param {Number} percent Percent of brighten (1-100) | ||
* @return {String|Boolean} Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -102,3 +117,3 @@ function lighten (color, percent) { | ||
const after = lighten(color, 20) | ||
// after = 'rgba(98,178,255,1)' | ||
// after = 'rgba(99,179,255,1)' | ||
``` | ||
@@ -117,5 +132,5 @@ | ||
* @description Adjust color opacity | ||
* @param {String} color Hex or rgb/rgba color | ||
* @param {Number} Percent of opacity | ||
* @return {String} Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @param {Number} percent Percent of opacity | ||
* @return {String|Boolean} Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -142,5 +157,5 @@ function fade (color, percent) { | ||
/** | ||
* @description Convert color to hex color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {String} Hex color | ||
* @description Convert color to Hex color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {String|Boolean} Hex color (Invalid input will return false) | ||
*/ | ||
@@ -163,6 +178,6 @@ function toHex (color) { | ||
/** | ||
* @description Convert color to rgb/rgba color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @description Convert color to Rgb|Rgba color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @param {Number} opacity The opacity of color | ||
* @return {String} Rgb/rgba color | ||
* @return {String|Boolean} Rgb|Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -187,5 +202,5 @@ function toRgb (color, opacity) { | ||
/** | ||
* @description Get the opacity of the color | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Number} Color opacity | ||
* @description Get the opacity of color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Number|Boolean} Color opacity (Invalid input will return false) | ||
*/ | ||
@@ -203,3 +218,2 @@ function getOpacity (color) { | ||
// opacity2 = 0.2 | ||
``` | ||
@@ -213,5 +227,5 @@ | ||
/** | ||
* @description Get the color rgb value | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Array} Rgb value of the color | ||
* @description Get the Rgb value of the color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Array<Number>|Boolean} Rgb value of the color (Invalid input will return false) | ||
*/ | ||
@@ -234,5 +248,5 @@ function getRgbValue (color) { | ||
/** | ||
* @description Get the color rgba value | ||
* @param {String} color Hex or rgb/rgba color | ||
* @return {Array} Rgba value of the color | ||
* @description Get the Rgba value of the color | ||
* @param {String} color Hex|Rgb|Rgba color or color keyword | ||
* @return {Array<Number>|Boolean} Rgba value of the color (Invalid input will return false) | ||
*/ | ||
@@ -258,5 +272,5 @@ function getRgbaValue (color) { | ||
/** | ||
* @description Get Color from rgb value | ||
* @param {Array} value Rgb color value | ||
* @return {String} Rgb / rgba color | ||
* @description Get Color from Rgb|Rgba value | ||
* @param {Array<Number>} value Rgb|Rgba color value | ||
* @return {String|Boolean} Rgb|Rgba color (Invalid input will return false) | ||
*/ | ||
@@ -275,1 +289,9 @@ function getColorFromRgbValue (value) { | ||
``` | ||
#### keywords | ||
<p align="center"> | ||
<img width="750px" src="./exampleImgs/keywords.png" /> | ||
</p> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
71640
12
271
1
282
2
7
+ Addedcore-js@^3.1.4
+ Addedcore-js@3.38.1(transitive)