0.0.64 • Published 12 months ago

color-adjuster v0.0.64

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

Color Adjuster

styled with prettier Greenkeeper badge Travis Coveralls Donate

A web color adjuster.

Features

Quick start

html code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" type="text/css" href="../dist/css/color-adjuster.css">
</head>
<body>
  <select id="colorMapSelector" style="position: absolute; left: 10px; top: 0px; width: 150px; height: 25px;"></select>
  <select id="alphaMapSelector" style="position: absolute; left: 10px; top: 30px; width: 150px; height: 25px;"></select>
  <button id="rgbCtrlButton" style="position: absolute; left: 10px; top: 60px; width: 150px; height: 25px;">添加rgb控制</button>
  <button id="alphaCtrlButton" style="position: absolute; left: 10px; top: 90px; width: 150px; height: 25px;">添加alpha控制</button>
  <button id="genButton" style="position: absolute; left: 10px; top: 120px; width: 150px; height: 25px;">生成序列</button>
  <button id="genColorMapButton" style="position: absolute; left: 10px; top: 150px; width: 150px; height: 25px;">生成颜色表</button>
  
  <div id="colorContainer"></div>
  <div id="colorMapContainer"></div>
  <script type="module" src="js/main.js"></script>
</body>
</html>

main.js code

You can import the generated bundle to use the whole library generated by this starter:

import {ColorMapAdjuster, ColorMapComponent} from "color-adjuster.module.js"

or

<script src="color-adjuster.js"></script>

then

import {ColorMapAdjuster, ColorMapComponent} from "../../../dist/color-adjuster.module.js"
import "../../../node_modules/jquery/dist/jquery.js";

let colorMapSelector; // 颜色表选择器
let alphaMapSelector; // 透明度选择器

let colorContainer = document.getElementById("colorContainer"); // rgba编辑器容器
let colorContainerWidth = 600;
let colorContainerHeight = 260;
colorContainer.style.width = colorContainerWidth.toString() + "px";
colorContainer.style.height = colorContainerHeight.toString() + "px";
colorContainer.style.left = "300px";
colorContainer.style.top = "200px";
colorContainer.style.position = "absolute";


// rgbaMap编辑器
let colorMapType = "rgba"; // "rgb" or "rgba"
let colorMapAdjuster = new ColorMapAdjuster({
    container: colorContainer, // rgba编辑器容器
    width: colorContainerWidth, // 容器宽度
    height: colorContainerHeight, // 容器高度
    padding: { // 容器内边距
        top: 5,
        left: 5,
        right: 5,
        bottom: 5
    },
    edge: 10, // 控制点大小
    strokeWidth: 2, // 控制点边距
    type: colorMapType  // "rgb" or "rgba"
});

// rgbMap编辑器
// colorContainerWidth = 600;
// colorContainerHeight = 160;
// colorContainer.style.width = colorContainerWidth.toString() + "px";
// colorContainer.style.height = colorContainerHeight.toString() + "px";
// let colorMapType = "rgb"; // "rgb" or "rgba"
// let colorMapAdjuster = new ColorMapAdjuster({
//     container: colorContainer, // rgba编辑器容器
//     width: colorContainerWidth, // 容器宽度
//     height: colorContainerHeight, // 容器高度
//     padding: { // 容器内边距
//         top: 5,
//         left: 5,
//         right: 5,
//         bottom: 5
//     },
//     edge: 10, // 控制点大小
//     strokeWidth: 2, // 控制点边距
//     type: colorMapType  // "rgb" or "rgba"
// });


let colorMapContainer = document.getElementById("colorMapContainer"); // rgba编辑器容器
let colorMapContainerWidth = 400;
let colorMapContainerHeight = 50;
colorMapContainer.style.width = colorMapContainerWidth.toString() + "px";
colorMapContainer.style.height = colorMapContainerHeight.toString() + "px";
colorMapContainer.style.left = "400px";
colorMapContainer.style.top = "600px";
colorMapContainer.style.position = "absolute";
let colorMapComponent = new ColorMapComponent({
    colorMapAdjuster: colorMapAdjuster,
    container: colorMapContainer
});

/**
 * 初始化颜色选择器
 * @param colorMapSelector
 */
function initColorMapSelector(colorMapSelector) {
    // colorMap数组
    let colorCategories = ["jet", "hsv"];
    // 组装颜色表选择器下拉列表
    colorMapSelector = document.getElementById("colorMapSelector");
    for (let i = 0; i < colorCategories.length; i++) {
        let c = colorCategories[i];
        colorMapSelector.options.add(new Option(c, c));
    }
    // 绑定颜色表选择器更改事件
    $("#colorMapSelector").change(function () {
        colorMapSelectorChange($("#colorMapSelector").val());
        alphaMapSelectorChange($("#alphaMapSelector").val());
    });
}

/**
 * 初始化透明度选择器
 * @param alphaMapSelector
 */
function initAlphaMapSelector(alphaMapSelector) {
    // 透明度数组
    let alphaCategories = [
        "Opaque",
        "MiddleBasinS7"
    ]
    // 组装透明度选择器下拉列表
    alphaMapSelector = document.getElementById("alphaMapSelector");
    for (let i = 0; i < alphaCategories.length; i++) {
        let c = alphaCategories[i];
        alphaMapSelector.options.add(new Option(c, c));
    }
    // 绑定透明度选择器更改事件
    $("#alphaMapSelector").change(function () {
        colorMapSelectorChange($("#colorMapSelector").val());
        alphaMapSelectorChange($("#alphaMapSelector").val());
    });
}

/**
 * 初始化控制按钮
 */
function initCtrlButton() {
    $("#rgbCtrlButton").click(function () {
        colorMapAdjuster.colorMapAdjuster.rgbEditor.insertCtrlComponent(null);
    });
    $("#alphaCtrlButton").click(function () {
        if(colorMapType === "rgba") {
            colorMapAdjuster.colorMapAdjuster.alphaEditor.insertAlphaCtrlComponent(null);
        }
    });
    $("#genButton").click(function () {
        let rgbaMapList = colorMapAdjuster.colorMapAdjuster.genColorMapList();
        console.info(rgbaMapList);
    });
    $("#genColorMapButton").click(function () {
        colorMapComponent.updateColorMapCanvas();
    });

}

/**
 * 透明通道对应的nodes
 * @param opt
 * @returns {*[]}
 */
function loadPresetTrendAlphaMap(opt) {
    let nodes = [];
    switch (opt) {
        case "Opaque":
            nodes.push([0, 1.]); // 表示无参考线,值全为1
            nodes.push([1, 1.]);
            break;
        case "MiddleBasinS7":
            nodes.push([0, 1.]);
            nodes.push([2. / 7., 1.]);
            nodes.push([3. / 7., 0]);
            nodes.push([4. / 7., 0]);
            nodes.push([5. / 7., 1.]);
            nodes.push([1., 1.]);
            break;
    }
    return nodes;
}

/**
 * 颜色表选择器更改事件
 * @param colorMapSelectorOpt
 */
function colorMapSelectorChange(colorMapSelectorOpt){
  let colorMapArr = {
    "jet": ['#000083', '#0277c4', '#1effe5', '#e6ff1a', '#fc4c00', '#800000'],
    "hsv": ['#ff0000', '#ceff02', '#00fb62', '#026bfe', '#c400fb', '#ff0006']
  }
    // 颜色表
    let colorMapArrTemp = colorMapArr[colorMapSelectorOpt];
    let customColorMapTemp = [];
    let step = 1 / (colorMapArrTemp.length - 1);
    for (let i = 0; i < colorMapArrTemp.length; i++) {
        customColorMapTemp.push({
            x: i * step,
            color: colorMapArrTemp[i]
        });
    }
    colorMapAdjuster.colorMapAdjuster.rgbEditor.updateRgbCtrlEditor(customColorMapTemp);
    if(colorMapType === "rgba") {
        colorMapAdjuster.colorMapAdjuster.rgbaMapEditor.updateRgbaMapCanvas();
    } else if(colorMapType === "rgb") {
        colorMapAdjuster.colorMapAdjuster.rgbMapEditor.updateRgbMapCanvas();
    }

}


/**
 * 透明度表选择器更改事件
 * @param alphaMapSelectorOpt
 */
function alphaMapSelectorChange(alphaMapSelectorOpt) {
    let alphaMapArrTemp = loadPresetTrendAlphaMap(alphaMapSelectorOpt);
    let customAlphaMapTemp = [];
    for (let i = 0; i < alphaMapArrTemp.length; i++) {
        let fixedFlag = false;
        if(i === 0 || i === alphaMapArrTemp.length -1) {
            fixedFlag = true;
        }
        customAlphaMapTemp.push({
            x: alphaMapArrTemp[i][0],
            y: alphaMapArrTemp[i][1],
            index: i,
            fixedX: fixedFlag
        });
    }

    if(colorMapType === "rgba") {
        colorMapAdjuster.colorMapAdjuster.alphaEditor.updateAlphaCtrlEditor(customAlphaMapTemp);
        colorMapAdjuster.colorMapAdjuster.rgbaMapEditor.updateRgbaMapCanvas();
    } else if(colorMapType === "rgb") {
        colorMapAdjuster.colorMapAdjuster.rgbMapEditor.updateRgbMapCanvas();
    }

}

/**
 * 初始化
 */
function init(){
    // 初始化颜色选择器
    initColorMapSelector(colorMapSelector);
    // 初始化透明度选择器
    initAlphaMapSelector(alphaMapSelector);
    // 初始化控制按钮
    initCtrlButton();
}

init();

NPM scripts

  • npm start: Run npm run build in watch mode
  • npm test: Run test suite
  • npm run test:watch: Run test suite in interactive watch mode
  • npm run test:prod: Run linting and generate coverage
  • npm run build: Generate bundles and typings, create docs
  • npm run lint: Lints code
  • npm run commit: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)

Excluding peerDependencies

On library development, one might want to set some peer dependencies, and thus remove those from the final bundle. You can see in Rollup docs how to do that.

Good news: the setup is here for you, you must only include the dependency name in external property within rollup.config.js. For example, if you want to exclude lodash, just write there external: ['lodash'].

Automatic releases

Prerequisites: you need to create/login accounts and add your project to:

Prerequisite for Windows: Semantic-release uses node-gyp so you will need to install Microsoft's windows-build-tools using this command:

npm install --global --production windows-build-tools

Setup steps

Follow the console instructions to install semantic release and run it (answer NO to "Do you want a .travis.yml file with semantic-release setup?").

Note: make sure you've setup repository.url in your package.json file

npm install -g semantic-release-cli
semantic-release-cli setup
# IMPORTANT!! Answer NO to "Do you want a `.travis.yml` file with semantic-release setup?" question. It is already prepared for you :P

From now on, you'll need to use npm run commit, which is a convenient way to create conventional commits.

Automatic releases are possible thanks to semantic release, which publishes your code automatically on github and npm, plus generates automatically a changelog. This setup is highly influenced by Kent C. Dodds course on egghead.io

Git Hooks

There is already set a precommit hook for formatting your code with Prettier :nail_care:

By default, there are two disabled git hooks. They're set up when you run the npm run semantic-release-prepare script. They make sure:

This makes more sense in combination with automatic releases

FAQ

Array.prototype.from, Promise, Map... is undefined?

TypeScript or Babel only provides down-emits on syntactical features (class, let, async/await...), but not on functional features (Array.prototype.find, Set, Promise...), . For that, you need Polyfills, such as core-js or babel-polyfill (which extends core-js).

For a library, core-js plays very nicely, since you can import just the polyfills you need:

import "core-js/fn/array/find"
import "core-js/fn/string/includes"
import "core-js/fn/promise"
...

What is npm install doing on first run?

It runs the script tools/init which sets up everything for you. In short, it:

  • Configures RollupJS for the build, which creates the bundles
  • Configures package.json (typings file, main file, etc)
  • Renames main src and test files

What if I don't want git-hooks, automatic releases or semantic-release??

Then you may want to:

  • Remove commitmsg, postinstall scripts from package.json. That will not use those git hooks to make sure you make a conventional commit
  • Remove npm run semantic-release from .travis.yml

What if I don't want to use coveralls or report my coverage?

Remove npm run report-coverage from .travis.yml

Credits

Made with by @rambo and all these wonderful contributors (emoji key):

Rambo💻 🔧📖

This project follows the all-contributors specification. Contributions of any kind are welcome!

0.0.40

12 months ago

0.0.41

12 months ago

0.0.42

12 months ago

0.0.43

12 months ago

0.0.44

12 months ago

0.0.45

12 months ago

0.0.46

12 months ago

0.0.47

12 months ago

0.0.37

12 months ago

0.0.38

12 months ago

0.0.39

12 months ago

0.0.30

12 months ago

0.0.31

12 months ago

0.0.32

12 months ago

0.0.33

12 months ago

0.0.34

12 months ago

0.0.35

12 months ago

0.0.36

12 months ago

0.0.26

12 months ago

0.0.27

12 months ago

0.0.28

12 months ago

0.0.29

12 months ago

0.0.62

12 months ago

0.0.64

12 months ago

0.0.20

12 months ago

0.0.21

12 months ago

0.0.22

12 months ago

0.0.23

12 months ago

0.0.24

12 months ago

0.0.25

12 months ago

0.0.60

12 months ago

0.0.61

12 months ago

0.0.59

12 months ago

0.0.15

12 months ago

0.0.16

12 months ago

0.0.17

12 months ago

0.0.18

12 months ago

0.0.19

12 months ago

0.0.51

12 months ago

0.0.52

12 months ago

0.0.53

12 months ago

0.0.54

12 months ago

0.0.55

12 months ago

0.0.56

12 months ago

0.0.57

12 months ago

0.0.58

12 months ago

0.0.14

12 months ago

0.0.50

12 months ago

0.0.48

12 months ago

0.0.49

12 months ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago