Skip to content

Commit 3a0ddea

Browse files
committedOct 22, 2023
ColorCodeConverter RGB-Hex Code
1 parent fc6372e commit 3a0ddea

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
 

‎ColorCodeConverter.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.Scanner;
2+
3+
public class ColorCodeConverter {
4+
public static void main(String[] args) {
5+
Scanner scanner = new Scanner(System.in);
6+
System.out.println("Color Code Converter");
7+
System.out.println("1. RGB to HEX");
8+
System.out.println("2. HEX to RGB");
9+
System.out.print("Enter your choice (1 or 2): ");
10+
11+
int choice = scanner.nextInt();
12+
scanner.nextLine(); // Consume the newline character
13+
14+
if (choice == 1) {
15+
System.out.print("Enter RGB color (e.g., 255, 0, 0): ");
16+
String rgb = scanner.nextLine();
17+
String hex = rgbToHex(rgb);
18+
System.out.println("HEX color: " + hex);
19+
} else if (choice == 2) {
20+
System.out.print("Enter HEX color (e.g., #FF0000): ");
21+
String hex = scanner.nextLine();
22+
String rgb = hexToRgb(hex);
23+
System.out.println("RGB color: " + rgb);
24+
} else {
25+
System.out.println("Invalid choice. Please select 1 or 2.");
26+
}
27+
}
28+
29+
public static String rgbToHex(String rgb) {
30+
String[] values = rgb.split(",");
31+
if (values.length != 3) {
32+
return "Invalid RGB format";
33+
}
34+
int r = Integer.parseInt(values[0]);
35+
int g = Integer.parseInt(values[1]);
36+
int b = Integer.parseInt(values[2]);
37+
38+
return String.format("#%02X%02X%02X", r, g, b);
39+
}
40+
41+
public static String hexToRgb(String hex) {
42+
if (hex.startsWith("#")) {
43+
hex = hex.substring(1);
44+
}
45+
46+
int intValue = Integer.parseInt(hex, 16);
47+
int r = (intValue >> 16) & 0xFF;
48+
int g = (intValue >> 8) & 0xFF;
49+
int b = intValue & 0xFF;
50+
51+
return r + ", " + g + ", " + b;
52+
}
53+
}

‎Lost and found application/.settings/org.eclipse.buildship.core.prefs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
arguments=
1+
arguments=--init-script /home/himanshu/.config/Code/User/globalStorage/redhat.java/1.23.0/config_linux/org.eclipse.osgi/55/0/.cp/gradle/init/init.gradle --init-script /home/himanshu/.config/Code/User/globalStorage/redhat.java/1.23.0/config_linux/org.eclipse.osgi/55/0/.cp/gradle/protobuf/init.gradle
22
auto.sync=false
33
build.scans.enabled=false
44
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
55
connection.project.dir=
66
eclipse.preferences.version=1
77
gradle.user.home=
8-
java.home=/nix/store/gpxl5qgg8kicmcvivwmybwjlpf7wh20l-graalvm17-ce-22.3.0
8+
java.home=/usr/lib/jvm/java-17-amazon-corretto
99
jvm.arguments=
1010
offline.mode=false
1111
override.workspace.settings=true

0 commit comments

Comments
 (0)