Files
JS/02_advanced/uebungen/u19_brightness-increase/index.html
Philippe Torrel f583bf92a3
2026-07-07 10:29:34 +02:00

99 lines
2.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Übung 19: Bildbearbeitung Helligkeit erhöhen</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<main>
<div class="container py-5">
<div class="alert alert-primary">
<h2>Übung 19: Bildbearbeitung Helligkeit erhöhen</h2>
<p>
Schreibe eine Funktion <code>increaseBrightness</code>, die die Helligkeit aller Pixel in einem 3D Bildarray
um einen bestimmten Wert erhöht. Stelle sicher, dass die RGB-Werte nicht über <code>255</code> hinausgehen.
</p>
<p>
Die Funktion nimmt ein dreidimensionales Array <code>image</code> und einen Wert <code>value</code> als
Parameter. Ziel ist es, die Helligkeit jedes Pixels im Bild zu erhöhen, indem jeder RGB-Wert um
<code>value</code> erhöht wird. Stelle dabe sicher, dass kein RGB-Wert den maximalen Wert von
<code>255</code> überschreitet.
</p>
</div>
</div>
</main>
<script>
'use strict';
'use strict';
const imageAr = [
[
// Row 1
[100, 150, 200], // Pixel 1
[50, 100, 150], // Pixel 2
],
[
// Row 2
[25, 75, 125], // Pixel 3
[200, 225, 250], // Pixel 4
],
];
const increaseBrightness = (imageAr = [], value) => {
// Your code here
return imageAr.map((row) => {
return row.map((rgbValue) => {
return rgbValue.map((elem) => {
const result = elem + value;
if (result > 255) {
return 255;
}
return result;
});
});
});
};
const increaseBrightness2 = (image, value) => {
const imageAr = image.map((row) => {
const rows = row.map((pixel) => {
const pixels = pixel.map((color) => {
return Math.min(255, color + value);
});
return pixels;
});
return rows;
});
return imageAr;
};
console.time('guard');
const newImage = increaseBrightness(imageAr, 30);
console.log(newImage);
console.timeEnd('guard');
console.time('Math');
const newImage2 = increaseBrightness2(imageAr, 30);
console.log(newImage2);
console.timeEnd('Math');
/*
Expected outcome:
[
[
[130, 180, 230],
[80, 130, 180],
],
[
[55, 105, 155],
[230, 255, 255],
],
]
*/
</script>
</body>
</html>