RGB (1) (Unterprogramme)
Schreiben Sie eine Funktion, die drei Zahlen (real) zwischen 0.0 und 1.0 entgegennimmt und eine RGB-kodierte Farbe (RGB = Rot, Grün, Blau) in 24 Bits in einem integer zurückgibt. Hier der geforderte Prototyp (auch Funktionsprototyp, Signatur, Funktionsdeklaration oder Funktionskopf genannt):
rgb(rot: real, gruen: real, blau: real): integer
Schreiben Sie dazu zunächst eine Funktion, die einen Wert zwischen 0.0 und 1.0 in eine integer-Zahl von 0 bis 255 verwandelt:
integer zahl := (integer) (wert * 256)
Beachten Sie, dass der Wert zwischen 0.0 und 1.0 liegt, dass speziell die Zahl Eins (1.0) nicht vorkommt. Dank dem Abrunden ist somit im Resultat tatsächlich 255 die größtmögliche Zahl. Nun ist es nötig, die drei Resultate in demselben integer abzulegen. Das erreichen wir, indem wir den Rotanteil mit 65 536 (= 2562) und den Grünanteil mit 256 multiplizieren; den Blauanteil können wir zum Schluss einfach addieren.
1 Kommentare
8 Lösung(en)
def rgb(rot,gruen,blau):
r =int(rot*256)
g =int(gruen*256)
b =int(blau*256)
return 256*256*r+256*g+b
*process langlvl(saa2);
*process limits(fixeddec(31));
*process aggregate, attributes;
*process flag(W), source, insource, options, xref,nest, number, offset;
*process gonumber, snap;
*process or('!');
/*********************************************************************/
/* Autor : philipp gressly freimann (@ http://www.sanits-training.ch)*/
/* Datum : 16. Nov. 2011 */
/* Aufgabe 5.2 (Programmieraufgaben.ch: RGB(1)) */
/*********************************************************************/
RGB1 : proc options(main noexecops);
dcl rot dec fixed(5, 4);
dcl gruen dec fixed(5, 4);
dcl blau dec fixed(5, 4);
dcl rgb_int dec fixed(31);
call einlesen();
rgb_int = rgb(rot, gruen, blau);
call ausgabe(rgb_int);
/****** Subroutinen *******/
einlesen: proc;
rot = teilLesen("rot");
gruen = teilLesen("gruen");
blau = teilLesen("blau");
end einlesen;
teilLesen: proc(Farbdimension) returns(dec fixed(5, 4));
dcl Farbdimension char(6);
dcl Eingabe char(128);
dcl EingabeF dec fixed(5, 4);
put skip list("Bitte ", Farbdimension,
" eingeben (0 <= x < 1.0): ");
get (Eingabe);
EingabeF = eingabe;
return (EingabeF);
end teilLesen;
rgb: proc(rot, gruen, blau) returns (dec fixed(31));
dcl (rot , gruen, blau) dec fixed(5, 4) ;
dcl (rInt, gInt, bInt) dec fixed(31) ;
rInt = mkInt(rot);
gInt = mkInt(gruen);
bInt = mkInt(blau);
return (bInt + (256 * (gInt + (256 * rInt))));
end rgb;
mkInt: proc(bruch) returns (dec fixed(31));
dcl bruch dec fixed(5, 4);
return (bruch * 256);
end mkInt;
ausgabe: proc(rgb);
dcl rgb dec fixed(31);
put skip list("Ausgabe des 24-Bit rgb als int : ");
put edit(rgb) (f(31));
put skip list("Ausgabe des 24-Bit rgb als binary: ");
put edit(rgb) (b);
end ausgabe;
end RGB1;
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
package ch.programmieraufgaben.methoden;
/**
* Aufgabe RGB (1) aus "Programmieren lernen"
* @author Philipp Gressly (phi AT gressly DOT ch)
*/
public class RGB1 {
public static void main(String[] args) {
new RGB1().top();
}
void top() {
test(0, 0.5, 0.99);
test(1/255.0, (255.0/256), 0);
test(0.9999, 0, 0.49999);
}
void test(double r, double g, double b) {
ausgabe(rgb(r, g, b), r, g, b);
}
void ausgabe(int rgb, double r, double g, double b) {
System.out.print (r + ", " + g + ", " + b + " -> ");
System.out.print (rgb);
System.out.println(" (hex: "+ Integer.toHexString(rgb) + ")");
}
int rgb(double r, double g, double b) {
int ri = (int) (256 * r);
int gi = (int) (256 * g);
int bi = (int) (256 * b);
return (int) (bi + 256 * (gi + (256 * ri)));
}
} // end of class RGB1
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
def prank():
lol is gar kein lösung xD
Lösung von: Name nicht veröffentlicht
function rgbFloatsToInts(arr) {
for (let i = 0; i < arr.length; i++)
arr[i] = Math.floor(arr[i] * 256);
return arr;
}
function rgbIntsToSingleInt(arr) {
return (arr[0] * 256 ** 2) + (arr[1] * 256) + arr[2];
}
console.log(rgbFloatsToInts([0.5, 0.5, 0.5]));
console.log(rgbIntsToSingleInt(rgbFloatsToInts([0.5, 0.5, 0.5])));
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
function rgbSingleIntToFloats(num, prec = 2) {
let rgb = [];
num = num.toString(2).padStart(32, '0').split('');
for (let i = 1; i <= 3; i++)
rgb.unshift(
(parseInt(num.splice(-8, 8).join(''), 2) / 256)
.toPrecision(prec)
);
return rgb;
}
// test
for (let i of [
0, // schwarz
8421504, // neutrales grau
(2 ** 8) - 1, // blau
(2 ** 32) - 1 // weiß
]) console.log( rgbSingleIntToFloats(i) );
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
var rgb = new RGB(0.5, 0.5, 0.5);
Console.WriteLine(rgb);
class RGB {
private readonly double _red, _green, _blue;
public RGB(double red, double green, double blue) {
_red = red;
_green = green;
_blue = blue;
}
private static double Test(double n) => n switch {
< 0 => 0,
> 1 => 256,
_ => n * 256,
};
public int Red => (int)Test(_red);
public int Green => (int)Test(_green);
public int Blue => (int)Test(_blue);
public int RGB_INT => 256 * 256 * Red + 256 * Green + Blue;
public override string ToString() => RGB_INT.ToString();
}
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
class RGB {
const double red_dbl_, green_dbl_, blue_dbl_;
constexpr size_t get_rgbValue(double n) const noexcept {
if (n < 0) return 0;
else if (n > 256) return 256;
else return n * 256;
}
public:
RGB(double red_dbl, double green_dbl, double blue_dbl) : red_dbl_{ red_dbl }, green_dbl_{ green_dbl }, blue_dbl_{ blue_dbl } {}
constexpr auto get_red_int() const noexcept { return get_rgbValue(red_dbl_); }
constexpr auto get_green_int() const noexcept { return get_rgbValue(green_dbl_); }
constexpr auto get_blue_int() const noexcept { return get_rgbValue(blue_dbl_); }
constexpr auto get_red_dbl() const noexcept { return red_dbl_; };
constexpr auto get_green_dbl() const noexcept { return green_dbl_; };
constexpr auto get_blue_dbl() const noexcept { return blue_dbl_; };
constexpr auto get_RGB_int() const noexcept { return 256LL * 256 * get_red_int() + 256 * get_green_int() + get_blue_int(); }
friend const std::ostream& operator<<(std::ostream& os, const RGB& rgb) noexcept {
os << "RGB (double): " << rgb.get_red_dbl() << ", " << rgb.get_green_dbl() << ", " << rgb.get_blue_dbl() << "\n";
os << "RGB (int): " << rgb.get_red_int() << ", " << rgb.get_green_int() << ", " << rgb.get_blue_int() << "\n";
os << "INT: " << rgb.get_RGB_int() << "\n";
return os;
}
};
int main() {
const RGB rgb{ 0.5, 0.5, 0.5 };
std::cout << rgb;
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
Mittleres Grau: (r, g, b) = (0.5, 0.5, 0.5) liefert 8 421 504 (als integer)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 0.5 |
Schwierigkeit: | k.A. |
Webcode: | 829e-g8uz |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |
Kommentare (1)