{"record":{"id":"7675a604c97d6f03","repo":"chinabugotech/hutool","slug":"rgb-must-be-0-255","errorCode":null,"errorMessage":"RGB must be 0~255!","messagePattern":"RGB must be 0~255!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/img/ColorUtil.java","lineNumber":50,"sourceCode":"\t * @return 16进制的颜色值，例如#fcf6d6\n\t * @since 4.1.14\n\t */\n\tpublic static String toHex(Color color) {\n\t\treturn toHex(color.getRed(), color.getGreen(), color.getBlue());\n\t}\n\n\t/**\n\t * RGB颜色值转换成十六进制颜色码\n\t *\n\t * @param r 红(R)\n\t * @param g 绿(G)\n\t * @param b 蓝(B)\n\t * @return 返回字符串形式的 十六进制颜色码 如\n\t */\n\tpublic static String toHex(int r, int g, int b) {\n\t\t// rgb 小于 255\n\t\tif (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {\n\t\t\tthrow new IllegalArgumentException(\"RGB must be 0~255!\");\n\t\t}\n\t\treturn String.format(\"#%02X%02X%02X\", r, g, b);\n\t}\n\n\t/**\n\t * 将颜色值转换成具体的颜色类型 汇集了常用的颜色集，支持以下几种形式：\n\t *\n\t * <pre>\n\t * 1. 颜色的英文名（大小写皆可）\n\t * 2. 16进制表示，例如：#fcf6d6或者$fcf6d6\n\t * 3. RGB形式，例如：13,148,252\n\t * </pre>\n\t * <p>\n\t * 方法来自：com.lnwazg.kit\n\t *\n\t * @param colorName 颜色的英文名，16进制表示或RGB表示\n\t * @return {@link Color}\n\t * @since 4.1.14","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/img/ColorUtil.java#L32-L68","documentation":"ColorUtil.toHex(int r, int g, int b) requires each channel in [0,255]. Any channel below 0 or above 255 throws IllegalArgumentException before formatting the hex color string.","triggerScenarios":"Passing RGB values read from a signed computation that overflow (e.g. from getRGB bit-twiddling), negative values from arithmetic, or un-sanitized user input parsed from a string like '300,0,0'.","commonSituations":"Manual color parsing from text fields; converting HSB/CMYK to RGB without clamping; feeding raw int channels from color pickers.","solutions":["Clamp each channel to [0,255] with Math.max(0, Math.min(255, v)) before calling toHex.","Validate/parse user-supplied RGB strings and reject or clamp out-of-range values explicitly.","When extracting channels from a packed int, mask with 0xff to guarantee range."],"exampleFix":"// before\nString hex = ColorUtil.toHex(r, g, b); // r may be 300 or -5\n// after\nString hex = ColorUtil.toHex(clamp(r), clamp(g), clamp(b));\n// where\nint clamp(int v){ return Math.max(0, Math.min(255, v)); }","handlingStrategy":"validation","validationCode":"int clamp(int v){ return Math.max(0, Math.min(255, v)); }\n// then ColorUtil.toHex(clamp(r), clamp(g), clamp(b))","typeGuard":"boolean validChannel(int v){ return v>=0 && v<=255; }","tryCatchPattern":"try { hex = ColorUtil.toHex(r,g,b); }\ncatch (IllegalArgumentException e){ if(e.getMessage().contains(\"0~255\")) { hex = ColorUtil.toHex(clamp(r),clamp(g),clamp(b)); } else throw e; }","preventionTips":["Always mask packed-int channels with 0xff.","Clamp outputs of any RGB-space conversion.","Validate parsed RGB strings before use."],"tags":["image","color","argument","range-check"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}