Change gray range from 0-255 to 0-1

pull/2/head
Andreas Gysin 2020-10-18 16:10:42 +02:00
rodzic a16b243a4e
commit 59b1a31b7f
5 zmienionych plików z 26 dodań i 18 usunięć

Wyświetl plik

@ -15,15 +15,15 @@ C64 and CGA palettes are exported as arrays
Colors in exported palettes are augmented to:
{
name: 'red',
r: 255,
g: 0,
b: 0,
a: 1.0,
gray: 54,
hex: '#FF0000',
css: 'rgb(255,0,0)'
int: 16711680,
name : 'red',
r : 255, // 0-255 (as in CSS)
g : 0, // 0-255 (as in CSS)
b : 0, // 0-255 (as in CSS)
a : 1.0, // 0-1 (as in CSS)
gray : 0.6, // 0-1 (custom)
hex : '#FF0000',
css : 'rgb(255,0,0)'
int : 16711680
}
*/
@ -74,9 +74,9 @@ export function rgb2hex(rgb) {
return '#' + r + g + b + a
}
// Convert {r,g,b} values to gray value [0-255]
// Convert {r,g,b} values to gray value [0-1]
export function rgb2gray(rgb) {
return Math.round(rgb.r * 0.2126 + rgb.g * 0.7152 + rgb.b * 0.0722)
return Math.round(rgb.r * 0.2126 + rgb.g * 0.7152 + rgb.b * 0.0722) / 255.0
}
// hex is not a string but an int number

Wyświetl plik

@ -53,7 +53,7 @@ const black = {
g : 0,
b : 0,
a : 1.0,
gray : 0
gray : 0.0
}
export class ImageBuffer {
@ -189,7 +189,6 @@ export class ImageBuffer {
return this.buffer[x + y * this.canvas.width]
}
// Debug -------------------------------------------------------------------
// Attaches the canvas to a target element for debug purposes
@ -274,8 +273,9 @@ function toBuffer(canvas, out){
return out
}
// Use this or import 'rgb2gray' from color.js
// https://en.wikipedia.org/wiki/Grayscale
function toGray(r,g,b) {
function toGray(r, g, b) {
return Math.round(r * 0.2126 + g * 0.7152 + b * 0.0722) / 255.0
}

Wyświetl plik

@ -22,8 +22,16 @@ pal.push(CSS3.lightblue)
export function pre(context, cursor, buffers){
// Double the height of the camera image
const ctxSizes = {cols : context.cols, rows : context.rows * 2}
cam.cover({...context, ...ctxSizes}, {x:1, y:2}).quantize(pal).mirrorX().write(buffers.data)
const newSize = {
cols : context.cols,
rows : context.rows * 2
}
// Adjust the scale to compensate
const adjustedScale = {
x:1,
y:2
}
cam.cover({...context, ...newSize}, adjustedScale).quantize(pal).mirrorX().write(buffers.data)
}
export function main(coord, context, cursor, buffers){

Wyświetl plik

@ -21,7 +21,7 @@ export function pre(context, cursor, buffers){
export function main(coord, context, cursor, buffers){
// Coord also contains the index of each cell:
const color = buffers.data[coord.index]
const index = Math.floor(color.gray / 255.0 * (density.length-1))
const index = Math.floor(color.gray * (density.length-1))
return density[index]
}

Wyświetl plik

@ -34,7 +34,7 @@ export function main(coord, context, cursor, buffers){
// Coord also contains the index of each cell
const color = buffers.data[coord.index]
// Add some chars to the output
const index = Math.floor(color.gray / 255.0 * (density.length-1))
const index = Math.floor(color.gray * (density.length-1))
return {
char : density[index],
color : 'white',