Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: colorscale as HTML Canvas #17

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions packages/ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,27 @@ function colorspaceOptions() {
chart2dColorspace.value = 'CAM02';
}

// Ramp function to create HTML canvas color scale
function ramp(color, n) {
n = window.innerHeight - 284;
let container = d3.select('#colorScale');
let canvas = container.append("canvas")
.attr("height", n)
.attr("width", 1);
let context = canvas.node().getContext("2d");

canvas.style.width = "40px";
canvas.style.imageRendering = "pixelated";
for (let i = 0; i < n; ++i) {
// only do this for actual colors
if(color[i] !== undefined) {
context.fillStyle = color[i]; // color[i / (n - 1)]
context.fillRect(0, i, 1, 1);
}
}
return canvas;
}

// Calculate Color and generate Scales
window.colorInput = colorInput;
function colorInput() {
Expand Down Expand Up @@ -512,6 +533,11 @@ function colorInput() {

// Generate scale data so we have access to all 3000 swatches to draw the gradient on the left
let scaleData = contrastColors.createScale({swatches: 3000, colorKeys: colorArgs, colorspace: mode, shift: shift});
let n = window.innerHeight - 282;
// let n = window.innerHeight - 84;
console.log(n);

let rampData = contrastColors.createScale({swatches: n, colorKeys: colorArgs, colorspace: mode, shift: shift});

newColors = contrastColors.generateContrastColors({colorKeys: colorArgs, base: background, ratios: ratioInputs, colorspace: mode, shift: shift});

Expand Down Expand Up @@ -560,15 +586,9 @@ function colorInput() {
swatch.style.backgroundColor = newColors[i];
}

// Generate Gradient
for (let i = 0; i < scaleData.colors.length; i++) {
var container = document.getElementById('colorScale');
var div = document.createElement('div');
div.className = 'colorScale-Item';
div.style.backgroundColor = scaleData.colors[i];

container.appendChild(div);
}
// Generate Gradient as HTML Canvas element
let filteredColors = rampData.colors;
ramp(filteredColors, n);

var backgroundR = d3.rgb(background).r;
var backgroundG = d3.rgb(background).g;
Expand Down