Professional Documents
Culture Documents
CODIGO FUENTE CitriCIELab
CODIGO FUENTE CitriCIELab
<!DOCTYPE html>
<html>
<head>
<title>Predicción de madurez de frutas cítricas en escala
de color CIELab</title>
<style>
canvas {
border: 1px solid black;
cursor: crosshair;
}
#pixelColor {
width: 50px;
height: 50px;
border: 1px solid black;
}
#colorBar {
width: 800px;
height: 40px;
}
</style>
</head>
<body>
<h1>Predicción de madurez de frutas cítricas en escala de
color CIELab</h1>
<input type="file" id="fileInput" accept="image/*" />
<br />
<canvas id="canvas" ></canvas>
<div id="colorBar"></div>
<label for="fruitType">Tipo de fruta:</label>
<select id="fruitType">
<option value="Lima">Lima</option>
<option value="Limon">Limon</option>
<option value="Naranja">Naranja</option>
<option value="Mandarina">Mandarina</option>
<option value="Toronja">Toronja</option>
</select>
<br />
<p id="maturityPrediction"></p>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const lValueInput = document.getElementById('lValue');
const aValueInput = document.getElementById('aValue');
const bValueInput = document.getElementById('bValue');
const pixelColorDiv = document.getElementById('pixelColor');
const fruitTypeSelect = document.getElementById('fruitType');
const colorBarDiv = document.getElementById('colorBar');
const maturityPredictionElement =
document.getElementById('maturityPrediction');
function handleFileSelect(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.addEventListener('click', handleCanvasClick);
}
img.src = e.target.result;
}
reader.readAsDataURL(file);
}
function handleCanvasClick(event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const pixel = ctx.getImageData(x, y, 1, 1).data;
const rgb = [pixel[0], pixel[1], pixel[2]];
const lab = rgb2lab(rgb);
lValueInput.value = lab[0].toFixed(2);
aValueInput.value = lab[1].toFixed(2);
bValueInput.value = lab[2].toFixed(2);
const pixelColor = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
pixelColorDiv.style.backgroundColor = pixelColor;
updateColorBar(lab[1], lab[2], fruitTypeSelect.value);
predictMaturity(lab[0], lab[1], lab[2],
fruitTypeSelect.value);
}
function rgb2lab(rgb) {
let r = rgb[0] / 255,
g = rgb[1] / 255,
b = rgb[2] / 255,
x, y, z;
fruitTypeSelect.addEventListener('change', updateColorBar);
function updateColorBar() {
const fruitType = fruitTypeSelect.value;
let colorBarGradient;
if (fruitType === 'Lima') {
// Colores de maduración para Limas
colorBarGradient = `linear-gradient(90deg, lab(33, -19,
23), lab(85, -25, 70), lab(67, -3, 45))`;
} else if (fruitType === 'Limon') {
// Colores de maduración para Limones
colorBarGradient = `linear-gradient(90deg, lab(42, -25,
37), lab(75, -30, 54), lab(82, -3, 61))`;
} else if (fruitType === 'Naranja') {
// Colores de maduración para naranjas
colorBarGradient = `linear-gradient(90deg, lab(44, -8,
50), lab(80, -4, 81), lab(71, 33, 76))`;
} else if (fruitType === 'Mandarina') {
// Colores de maduración para Mandarinas
colorBarGradient = `linear-gradient(90deg, lab(35 -26,
35), lab(59, 7, 52), lab(52, 52, 60))`;
} else if (fruitType === 'Toronja') {
// Colores de maduración para Toronjas
colorBarGradient = `linear-gradient(90deg, lab(51, -16,
50), lab(75, -2, 69), lab(69, 26, 63))`;
}
colorBarDiv.style.background = colorBarGradient;
}
</script>
</body>
</html>