Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 1x 8x 8x 4x 4x 4x 4x 2x 2x 2x 4x 4x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 1x 1x 1x 1x | import * as Inputs from "../inputs";
import { MathBitByBit } from "./math";
export class Color {
constructor(private readonly math: MathBitByBit) { }
/**
* Creates and returns a hex color string (pass-through for color input).
* Example: '#FF5733' → '#FF5733'
* @param inputs Color hex
* @returns color string
* @group create
* @shortname color
* @drawable false
*/
hexColor(inputs: Inputs.Color.HexDto): Inputs.Base.Color {
return inputs.color;
}
/**
* Converts hex color to RGB object with r, g, b values (0-255 range).
* Example: '#FF5733' → {r: 255, g: 87, b: 51}
* @param inputs Color hex
* @returns rgb color
* @group convert
* @shortname hex to rgb
* @drawable false
*/
hexToRgb(inputs: Inputs.Color.HexDto): Inputs.Base.ColorRGB {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(inputs.color);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : undefined;
}
/**
* Converts RGB values to hex color string (supports custom min/max ranges, auto-remaps to 0-255).
* Example: r=255, g=87, b=51 with range [0,255] → '#ff5733'
* Example: r=1, g=0.5, b=0.2 with range [0,1] → '#ff7f33'
* @param inputs Color hext
* @returns hex color
* @group convert
* @shortname rgb to hex
* @drawable false
*/
rgbToHex(inputs: Inputs.Color.RGBMinMaxDto): Inputs.Base.Color {
let r = inputs.r;
let g = inputs.g;
let b = inputs.b;
// sometimes rgb values are in 0 - 100 or 0 - 1 ranges
// so we need to remap them to 0 - 255
if (inputs.max !== 255) {
r = Math.round(this.math.remap({ number: r, fromLow: inputs.min, fromHigh: inputs.max, toLow: 0, toHigh: 255 }));
g = Math.round(this.math.remap({ number: g, fromLow: inputs.min, fromHigh: inputs.max, toLow: 0, toHigh: 255 }));
b = Math.round(this.math.remap({ number: b, fromLow: inputs.min, fromHigh: inputs.max, toLow: 0, toHigh: 255 }));
}
const s = `#${Number(0x1000000 + r * 0x10000 + g * 0x100 + b).toString(16).substring(1, 7)}`;
return s;
}
/**
* Converts RGB object to hex color string (supports custom min/max ranges).
* Example: {r: 1, g: 0.5, b: 0.2} with range [0,1] → '#ff7f33'
* @param inputs Color hext
* @returns hex color string
* @group convert
* @shortname rgb obj to hex
* @drawable false
*/
rgbObjToHex(inputs: Inputs.Color.RGBObjectMaxDto): Inputs.Base.Color {
return this.rgbToHex({ r: inputs.rgb.r, g: inputs.rgb.g, b: inputs.rgb.b, min: inputs.min, max: inputs.max });
}
/**
* Converts hex color to RGB and remaps values to a custom range.
* Example: '#FF5733' mapped to [0,1] → {r: 1, g: 0.341, b: 0.2}
* Example: '#FF5733' mapped to [0,100] → {r: 100, g: 34.1, b: 20}
* @param inputs Color hext
* @returns rgb color
* @group convert
* @shortname hex to rgb mapped
* @drawable false
*/
hexToRgbMapped(inputs: Inputs.Color.HexDtoMapped): Inputs.Base.ColorRGB {
const rgb = this.hexToRgb(inputs);
return {
r: this.math.remap({ number: rgb.r, fromLow: 0, fromHigh: 255, toLow: inputs.from, toHigh: inputs.to }),
g: this.math.remap({ number: rgb.g, fromLow: 0, fromHigh: 255, toLow: inputs.from, toHigh: inputs.to }),
b: this.math.remap({ number: rgb.b, fromLow: 0, fromHigh: 255, toLow: inputs.from, toHigh: inputs.to }),
};
}
/**
* Extracts the red channel value from hex color (can be mapped to custom range).
* Example: '#FF5733' with range [0,1] → 1
* @param inputs Color hext
* @returns rgb color
* @group hex to
* @shortname red
* @drawable false
*/
getRedParam(inputs: Inputs.Color.HexDtoMapped): number {
const rgb = this.hexToRgbMapped(inputs);
return rgb.r;
}
/**
* Extracts the green channel value from hex color (can be mapped to custom range).
* Example: '#FF5733' with range [0,1] → 0.341
* @param inputs Color hext
* @returns rgb color
* @group hex to
* @shortname green
* @drawable false
*/
getGreenParam(inputs: Inputs.Color.HexDtoMapped): number {
const rgb = this.hexToRgbMapped(inputs);
return rgb.g;
}
/**
* Extracts the blue channel value from hex color (can be mapped to custom range).
* Example: '#FF5733' with range [0,1] → 0.2
* @param inputs Color hext
* @returns blue param
* @group hex to
* @shortname blue
* @drawable false
*/
getBlueParam(inputs: Inputs.Color.HexDtoMapped): number {
const rgb = this.hexToRgbMapped(inputs);
return rgb.b;
}
/**
* Extracts the red channel value from RGB object.
* Example: {r: 255, g: 87, b: 51} → 255
* @param inputs Color rgb
* @returns red param
* @group rgb to
* @shortname red
* @drawable false
*/
rgbToRed(inputs: Inputs.Color.RGBObjectDto): number {
return inputs.rgb.r;
}
/**
* Extracts the green channel value from RGB object.
* Example: {r: 255, g: 87, b: 51} → 87
* @param inputs Color rgb
* @returns green param
* @group rgb to
* @shortname green
* @drawable false
*/
rgbToGreen(inputs: Inputs.Color.RGBObjectDto): number {
return inputs.rgb.g;
}
/**
* Extracts the blue channel value from RGB object.
* Example: {r: 255, g: 87, b: 51} → 51
* @param inputs Color rgb
* @returns blue param
* @group rgb to
* @shortname blue
* @drawable false
*/
rgbToBlue(inputs: Inputs.Color.RGBObjectDto): number {
return inputs.rgb.b;
}
/**
* Inverts a hex color (flips RGB channels: 255-r, 255-g, 255-b).
* With blackAndWhite=true → returns '#000000' or '#ffffff' based on brightness.
* Example: '#FF5733' → '#00a8cc', '#FF5733' with blackAndWhite=true → '#ffffff'
* @param inputs hex color and black and white option
* @returns inverted color
* @group hex to
* @shortname invert color
* @drawable false
*/
invert(inputs: Inputs.Color.InvertHexDto): Inputs.Base.Color {
const { r, g, b } = this.hexToRgbMapped({ color: inputs.color, from: 0, to: 255 });
if (inputs.blackAndWhite) {
return (r * 0.299 + g * 0.587 + b * 0.114) > 186
? "#000000"
: "#ffffff";
}
const rInv = (255 - r);
const gInv = (255 - g);
const bInv = (255 - b);
return this.rgbToHex({ r: rInv, g: gInv, b: bInv, min: 0, max: 255 });
}
}
|