coding UTF-8; // Prevent character corruption // Import required libraries import GUI; import Math; import Text; import Color; import system.Int; // Values for adjusting the GUI layout const int WINDOW_WIDTH = 460; const int WINDOW_HEIGHT = 300; const int MARGIN = 10; const int FIELD_WIDTH = 140; const int FIELD_HEIGHT = 30; const int BUTTON_WIDTH = 100; const int BUTTON_HEIGHT = 45; const int DISPLAY_WIDTH = WINDOW_WIDTH - 40; const int DISPLAY_HEIGHT = WINDOW_HEIGHT - FIELD_HEIGHT*3 - 80; // Variables to hold the IDs of GUI components int window; int displayPanel; int redField; int greenField; int blueField; int codeField; int rgbToCodeButton; int codeToRgbButton; // This function is automatically called at program startup // (Note: The event-handling flow when a button is clicked // starts from the event handler functions at the end of the code.) void main(){ // Build the GUI createComponent(); // Hide the console window hide(); // Set initial values string code = "000000"; setColorByCode(code); } // Function to set the color from a color code string void setColorByCode(string inCode){ // Set the value in the CODE field setCodeField(inCode); // Remove leading "#" if present if(startsWith(inCode, "#")){ inCode = cropText(inCode, 1, countText(inCode)); } // Add hexadecimal prefix inCode = "0x" + inCode; // Validate the string as hexadecimal if(isHex(inCode)){ // Convert to int int code = inCode; // Check if it's within the valid color code range if(0<=code && code<=0xffffff){ // Generate a color array int color[] = color(code); // Apply the color to the display panel setDisplayColor(color); // Set values in the RED/GREEN/BLUE fields setRgbField( (string)color[0], (string)color[1], (string)color[2] ); }else{ alert("The CODE value is outside the valid range of #000000 to #ffffff."); } }else{ alert("The CODE value is not a valid hexadecimal number."); } } // Function to set the color from RGB value strings void setColorByRgb(string inR, string inG, string inB){ // Set values in the RED/GREEN/BLUE fields setRgbField(inR, inG, inB); // Check if the input values are valid integers if( isInt(inR) && isInt(inG) && isInt(inB) ){ // Convert to int int r = inR; int g = inG; int b = inB; // Check if the values are within valid RGB range if(0<=r&&r<=255 && 0<=g&&g<=255 && 0<=b&&b<=255){ // Create a color array int color[] = {r, g, b, 255}; // Apply the color to the display panel setDisplayColor(color); // Get the color code string code = hex( getColorCode(color) ); code = formatColorCode(code); // Set value in the CODE field setCodeField(code); }else{ alert("RED / GREEN / BLUE values must be in the range of 0 to 255."); } }else{ alert("RED / GREEN / BLUE values must be integers."); } } // Function to convert hex string with "0x" prefix into 6-digit color code format // (called from setColorByRgb) string formatColorCode(string code){ // Get original string length int length = countText(code); // Remove the "0x" prefix code = cropText(code, 2, length); length -= 2; // Calculate how many leading zeros are needed int zeros = 6 - length; // Pad the beginning with zeros to make it 6 digits for(int i=0; i>"); mountComponent(rgbToCodeButton, panel); codeToRgbButton = newButton(0, 0, 0, 0, "<<"); mountComponent(codeToRgbButton, panel); return panel; } // ====================================================================== // The following are GUI update routines // ====================================================================== // Function to repaint GUI components void repaint(){ paintComponent(displayPanel); // Repaint the color display panel paintComponent(window); // Repaint the main window } // Function to set the color of the display panel void setDisplayColor(int color[]){ int fgColor[] = {0, 0, 0, 0}; // Foreground color (not used for panels) setComponentColor(displayPanel, fgColor, color); // Set color to the display panel repaint(); // Redraw the screen } // Function to set the RGB input fields void setRgbField(string r, string g, string b){ setComponentText(redField, r); // Set value to RED field setComponentText(greenField, g); // Set value to GREEN field setComponentText(blueField, b); // Set value to BLUE field repaint(); // Redraw the screen } // Function to set the CODE input field private void setCodeField(string code){ setComponentText(codeField, code); // Set value to CODE field repaint(); // Redraw the screen } // ====================================================================== // The following are event handler functions // ====================================================================== // Called when a button is clicked void onButtonClick(int componentID){ // If the ">>" button was clicked (RGB -> CODE) if(componentID == rgbToCodeButton){ // Get the values from RED / GREEN / BLUE fields and update color string r = getComponentText(redField); string g = getComponentText(greenField); string b = getComponentText(blueField); setColorByRgb(r, g, b); // If the "<<" button was clicked (CODE -> RGB) }else if(componentID == codeToRgbButton){ // Get the value from the CODE field and update color string colorCode = getComponentText(codeField); setColorByCode(colorCode); } } // Called when the window is closed void onWindowClose(int componentID){ // If the main window was closed, exit the program if(componentID == window){ exit(); } }