{"record":{"id":"af91e3134a3ab7c9","repo":"Yalantis/uCrop","slug":"rgbtohsl-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoHSL(): Instance is not a RGB image.","messagePattern":"RGBtoHSL\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37142,"sourceCode":"        case 4 : R = X; G = 0; B = C; break;\n        default : R = C; G = 0; B = X;\n        }\n        p1[N] = (T)((R + m)*3*255);\n        p2[N] = (T)((G + m)*3*255);\n        p3[N] = (T)((B + m)*3*255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from HSI to RGB color spaces \\newinstance.\n    CImg<Tfloat> get_HSItoRGB() const {\n      return CImg< Tuchar>(*this,false).HSItoRGB();\n    }\n\n    //! Convert pixel values from RGB to HSL color spaces.\n    CImg<T>& RGBtoHSL() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoHSL(): Instance is not a RGB image.\",\n                                    cimg_instance);\n\n      T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);\n      const longT whd = (longT)width()*height()*depth();\n      cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          R = (Tfloat)p1[N],\n          G = (Tfloat)p2[N],\n          B = (Tfloat)p3[N],\n          m = cimg::min(R,G,B),\n          M = cimg::max(R,G,B),\n          C = M - m,\n          H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),\n          L = 0.5f*(m + M)/255,\n          S = L==1 || L==0?0:C/(1 - cimg::abs(2*L - 1))/255;\n        p1[N] = (T)H;","sourceCodeStart":37124,"sourceCodeEnd":37160,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37124-L37160","documentation":"CImg<T>::RGBtoHSL() converts pixel values from RGB to the HSL color space and requires exactly 3 channels (R, G, B planes). Images with any other spectrum throw CImgInstanceException. The check verifies channel count only — 3-channel non-RGB data will convert without error but yield wrong colors.","triggerScenarios":"Calling img.RGBtoHSL() on grayscale (spectrum=1), RGBA (spectrum=4), or otherwise non-3-channel images.","commonSituations":"Single-channel medical/scientific images; PNG with alpha; images loaded via loaders that return indexed or grayscale formats; forgetting channels(0,2) after compositing with an alpha layer.","solutions":["Check img.spectrum()==3 first; convert grayscale to 3 channels by replication.","For RGBA, call img.channels(0,2) to drop alpha before converting.","Use get_RGBtoHSL() on a properly prepared copy if the original must stay untouched.","Normalize image loading so all inputs are converted to RGB once, at load time."],"exampleFix":"// before\nCImg<unsigned char> img(\"scan.png\"); // grayscale, spectrum=1\nimg.RGBtoHSL(); // throws\n// after\nCImg<unsigned char> img(\"scan.png\");\nif (img.spectrum() == 1) img.resize(-100,-100,-100,3); // replicate gray to RGB\nimg.RGBtoHSL();","handlingStrategy":"type-guard","validationCode":"template<typename T> bool isRgb(const CImg<T>& img) { return img.spectrum() == 3; }\nif (isRgb(img)) img.RGBtoHSL();","typeGuard":"template<typename T> bool isRgb(const CImg<T>& img) { return img.spectrum() == 3; }","tryCatchPattern":"try {\n  img.RGBtoHSL();\n} catch (const CImgInstanceException& e) {\n  std::fprintf(stderr, \"RGBtoHSL requires 3 channels, image has %d\\n\", img.spectrum());\n}","preventionTips":["Guard every conversion with a spectrum()==3 check.","Normalize inputs to RGB at load time.","Drop alpha channels early in the pipeline.","Track spectrum changes through resize/channel operations."],"tags":["cimg","colorspace","image-processing","channel-count"],"backgroundTag":"incompatible-source-type","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}