Eclipse java write a program that computes a css color.

EBK JAVA PROGRAMMING
8th Edition
ISBN:9781305480537
Author:FARRELL
Publisher:FARRELL
Chapter16: Graphics
Section: Chapter Questions
Problem 7RQ
icon
Related questions
Question
Eclipse java write a program that computes a css color.
Expert Solution
Step 1: algorithm

// function to convert decimal to hexadecimal
static String decToHexa(int n) {
// char array to store hexadecimal number
char[] hexaDeciNum = new char[2];

// counter for hexadecimal number array
int i = 0;
while (n != 0) {

// temporary variable to store remainder
int temp = 0;

// storing remainder in temp variable.
temp = n % 16;

// check if temp < 10
if (temp < 10) {
hexaDeciNum[i] = (char) (temp + 48);
i++;
} else {
hexaDeciNum[i] = (char) (temp + 55);
i++;
}

n = n / 16;
}

String hexCode = "";
if (i == 2) {
hexCode += hexaDeciNum[0];
hexCode += hexaDeciNum[1];
} else if (i == 1) {
hexCode = "0";
hexCode += hexaDeciNum[0];
} else if (i == 0)
hexCode = "00";

// Return the equivalent
// hexadecimal color code
return hexCode;
}

// Function to convert the
// RGB code to Hex color code
static String convertRGBtoHex(int R, int G, int B) {
if ((R >= 0 && R <= 255) && (G >= 0 && G <= 255) && (B >= 0 && B <= 255)) {

String hexCode = "#";
hexCode += decToHexa(R);
hexCode += decToHexa(G);
hexCode += decToHexa(B);

return hexCode;
}

// The hex color code doesn't exist
else
return "-1";
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Rendering
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT