## Introduction

Optoelectronic devices that provide light responses and spectral measurements are promising components for use in a wide range of applications, including multifunctional sensing, colour imaging, high-bandwidth optical communications, and in situ characterization techniques1,2,3,4,5,6,7,8,9,10,11,12,13,14. However, the current selection of optoelectronic detectors is used to measure only light intensities (power densities, p)1,2,3,4,5,6,7,8,9. Most photodetectors can be classified into thermal and semiconductive types according to their working principles. The former, combining thermopiles with light absorbers, converts light to heat and then to electrical signals indirectly indicating light power densities10,11,12. The latter, directly converting photons to excitons, provides electrical signals depending on the photon flux φ and energy ε2,7,8. For a constant φ, the response of a semiconductor to ε is dependent on its light absorption A(ε) and photon energy-dependent quantum efficiency η(ε) for the absorbed photons. Both A(ε) and η(ε) exhibit fluctuating curves for semiconductors with indirect bandgaps (e.g., silicon) and step-like curves for those with direct bandgaps. This means that semiconductive photodetectors either give fluctuating responses to ε or provide only limited information about ε. Ordinary semiconductive photodetectors can only output light power densities by combining photon flux and energy (p = εφ) after calibration, and most often, the user must input the current wavelength (e.g., commercial silicon photodiodes). Moreover, a narrowband photodetector is only sensitive to specific lights over a rather small spectral range;3,13 these narrowband photodetectors generally exhibit indiscriminate sensitivity to lights over their detectable spectral range and are blind to the spectrum beyond their detectable wavelengths. Thus, none of the existing photodetectors are able to provide spectral information at the device level. It was recently reported that integrating dozens or hundreds of photodetectors with semiconductors presenting different bandgaps can reconstruct the spectral curves of incident light14,15. Nevertheless, such an approach already requires a chip-level device assembly and signal-processing system, and can generate redundant signals for applications that do not need detailed spectral information.

Our brain perceives light as a combination of colours and intensities, which allows it to process a large number of light signals with high efficiency16,17,18,19. Inspired by this, the projection of spectra into representative indices may enable true and efficient spectrum processing at the device level. Studies on colour perception suggest that while the human eye perceives three distinct wavelengths (red, green, and blue), the human brain projects the colours in terms of hue and intensity, where the colour is best represented by the hue16,18,20. Therefore, a device for spectrum processing and colour perception should be capable of the following: (1) discriminatively collect a light signal in different spectral regions—the more signals from different spectral regions, the higher the hue resolution is and (2) output compressed electrical signals directly indicating the hue and intensity of the incident light.

In this work, we propose a concept of spectrum projection and colour perception through the use of a single device by introducing bandgap-gradient semiconductors as photoactive materials. Since photon absorption can only occur at energies above the bandgap of semiconductors, the devices can sense the spectral content of light signals with high resolution. Halide perovskite films with a continuous gradient of direct bandgaps are developed to demonstrate a device that outputs a signal exhibiting linear responses to both ε and φ. Combining the other output signal that is linearly dependent only on φ, the intensity and hue (i.e., total photon flux and average photon energy) of spectra can be obtained from a single device. The mechanical flexibility of the devices is demonstrated by fabricating them on polymer substrates. The online colour perception is shown by using the device to sense real-time changes in lighting. Finally, sensing applications are demonstrated by using the device to perceive the colour changes of stimuli-sensitive materials that are responsive to chemical/bio-analytes.

## Results

### Design of colour-perception device

Figure 1a shows the diagram of a spectrum containing photons with different energies. The photon flux for a given photon energy εi is defined as φi, and the total power density is given by $$p = \mathop {\sum}\nolimits_i {\varepsilon _i\varphi _i}$$. Statistically, representative indices for a spectrum should include the total photon flux $$\varphi = \mathop {\sum}\nolimits_i {\varphi _i}$$ and the average photon energy weighted by the flux $$\bar \varepsilon _\varphi = \mathop {\sum}\nolimits_i {\varepsilon _i\varphi _i} /\mathop {\sum}\nolimits_i {\varphi _i = p/\varphi }$$. Such indices are critical to spectrum identifications; that is, $$\bar \varepsilon _\varphi$$ can be clearly different for the spectra even with the same p, as illustrated in Fig. 1b. An ideal colour-perception device should be able to discriminatively absorb the photon flux corresponding to each photon energy in the spectrum and project them into output signals that directly indicate $$\bar \varepsilon _\varphi$$ and φ. Accordingly, we propose the device design shown in Fig. 1c. The device consists of two parts and outputs two signals, in which one part uses a semiconductor with a rather small bandgap (SBG) as a photoactive layer and exhibits an output current (I1) determined only by φ in a certain ε region. The other part is composed of semiconductors with gradient bandgaps (GBGs) as a photosensing layer, and a high photon energy ε can lead to a large photoexcited domain (domains with εb ≤ ε can be photoexcited); hence, the photon flux for each photon energy can be distinguished based on the extent of the photoexcited domain. When the bandgap gradient is uniform, a constant ε variation can lead to a fixed change in the photoexcited domain. Thus, this part of the device may present a linear response to ε and is expected to output a current (I2) that reflects the $$\bar \varepsilon _\varphi$$ of spectra. Combining the two output currents (I1 and I2), the spectral information may be projected into statistical spectral indices $$\bar \varepsilon _\varphi$$ and φ. On the other hand, the human visual system only perceives red, green, and blue and projects these colours in terms of hue (Fig. 1d). Labelling the flux of red, green, and blue photons as R, G, and B, respectively, the hue is defined as:

$$h = \frac{1}{{2\pi }}{\mathrm{atan2}}\left( {\sqrt {3}\left({G - B}\right), R - G - B}\right)$$
(1)

Figure 1e shows that the hue and $$\bar \varepsilon _\varphi$$ of a spectrum composed of R, G, and B are strikingly similar from the red to blue region. Therefore, except for the unphysical red–purple–blue quadrant, it is expected that a device measuring $$\bar \varepsilon _\varphi$$ will yield a fairly good approximation for the hue of light and thus achieve colour perception.

Semiconductors with GBGs can be achieved with compositionally graded alloys or compounds. All-inorganic halide perovskites (CsPbX3) exhibit tunable direct bandgaps over the whole visible range (1.7–3.1 eV), outstanding optoelectronic properties and solution processability2,3,6,21,22,23,24,25,26,27. Compared to organic–inorganic hybrid perovskites (e.g., MAPbX3 and FAPbX3), all-inorganic perovskites exhibit better robustness and environmental stability28. Although the cubic perovskite phase of CsPbI3 is relatively unstable, CsPbI3 can remain in the cubic perovskite phase at room temperature for a period of time, while exhibiting excellent optoelectronic properties29. Furthermore, CsPbI3 perovskites obtained from a halide exchanging CsPbBr3 present better phase stability than directly synthesized perovskites30. Thus, we use CsPbX3 perovskites to demonstrate this proof-of-concept. A halogen-exchange method via dipping is developed to obtain perovskite films with a continuous halide-composition gradient as shown in Fig. 2a. Figure 2b shows the fluorescence microscopy photos of a series of CsPbBr3 films on SiO2 exchanged with CsI solutions after the dipping process. According to the pictures, the photoluminescent hue of the exchanged films is spectrally continuous, changing from green to red under UV light (namely, rainbow perovskites). Similarly, the hue of CsPbBr3 films exchanged with CsCl solutions varies gradually from green to dark blue (Fig. 2c). Since the photoluminescence (PL) colours of perovskites depend mainly on their bandgaps3,21,31, these PL measurements are strong evidence that these rainbow perovskite films present continuously varying bandgaps. In this dipped exchange process, the bottom sections of the CsPbBr3 films undergo halide exchange with the CsCl or CsI solution for a longer duration than the top sections, spontaneously forming perovskite films with a gradient halide composition, i.e., GBGs. Interestingly, the length of the GBG range (LGBG) can be increased by increasing the dipping speed. From left to right in Fig. 2b, the dipping speed increases from 1 to 5 mm/s, and thus, the corresponding LGBG increases from 35 μm to several millimetres. The micromorphologies of the rainbow perovskites are observed by scanning electron microscopy (SEM). Figure 2d presents the cross-section SEM image of the rainbow perovskite, showing a film thickness of ~100 nm. This thin film ensures uniform halogen exchange between the surface and the bulk of the film31. At different positions on the rainbow perovskite (marked in Fig. 2e), the SEM images present similar morphologies of connected grains with rather high coverage (Fig. 2f and Supplementary Fig. S1). These results reveal that the effect of the dipping process on the perovskite micromorphology is limited. The energy dispersive spectroscopy (EDS) linear and mapping scans of elemental distributions were also carried out on the GBG films around the marked positions (Fig. 2g and Supplementary Fig. S1). At position , clear distributions of Br, Cs and Pb can be observed, while the presence of I is negligible. From position to , the concentration of Br continuously decreases and that of I continues to increase, while the amounts of Cs and Pb remain constant. No obvious halide-phase separation was found according to the EDS mapping images. This result indicates successful halogen exchange during the dipping process.

When the exchanging solutions are diluted or the GBG perovskites are fabricated on polymer films, the LGBG can be further increased. According to the UV–visible absorption, crystalline behaviour and PL spectrum (Supplementary Figs. S2 and S3), these rainbow perovskite films with a large LGBG also display more spread-out bandgap gradients. The XRD spectrum around position “Br” clearly shows characteristic diffraction peaks at 15.2 and 30.7°, which can be attributed to the (100) and (200) orientations of the cubic CsPbBr3 crystal. After exchange with Cl or I ions, the XRD peaks exhibit obvious shifts to larger or smaller 2θ values (Supplementary Fig. S2c). This is ascribed to the changed lattice constants incurred by the exchange between the Br of CsPbBr3 and the Cl or I in the solution31,32. In brief, the above results indicate that the CsPbX3 film mainly retains a cubic crystal phase and direct bandgap after undergoing the halogen-exchange process via dipping. The weak diffraction peaks at ~27° for the Br/I perovskite and at ~27 and 45° for the Br/Cl perovskite can be assigned to a few orthorhombic and non-perovskite CsPb2X5 phases that are formed during the dipping process33. The mechanism of the dipping exchange process is discussed in Supplementary Fig. S4. Overall, a successful fabrication of GBG semiconductor films is displayed.

To further investigate the optoelectronic characteristics of the GBG semiconductor along the direction of the bandgap gradient, multiple devices are fabricated at different locations on the rainbow perovskite film with an LGBG of ~4 mm. The photoconductivity response curves under monochromatic light exhibit a sharp cutoff for wavelengths varying from 470 to 690 nm, which is consistent with the bandgap gradient (Supplementary Fig. S5). However, the devices with ion-exchanged perovskites exhibit a high resistance, which may be due to the formation of a few non-cubic-perovskite phases as mentioned above33. The difference in the conductivities of the GBG composites is likely to deteriorate the hue perception of the devices. Thus, an organic semiconductor, dioctylbenzothieno [2,3-b] benzothiophene (C8BTBT), is introduced to construct heterojunctions with the rainbow perovskites34,35, and similar conductivities are then achieved on the heterojunctions of rainbow perovskites with different bandgaps (Supplementary Figs. S6S8). Note that we aim to demonstrate the colour-perception devices, thus, the conducting and intensity-sensing properties of the rainbow perovskites are not optimized here.

### Linear response to photon energy and device modelling

Br/I rainbow perovskite films with an LGBG of ~1.5 mm are employed to fabricate the heterojunctions, and only single devices are built on the GBG films, as shown in Fig. 3a. The devices, with an electric current direction parallel to the bandgap-gradient direction (i.e., parallel devices), are named Devices I, II and III, corresponding to electrode intervals of 0.5, 1, and 1.5 mm, respectively. According to the time-dependent current curves (I–t) of Device I under pulsed monochromatic illumination at different wavelengths (Fig. 3b), the photocurrent decreases with increasing wavelengths from 520 to 560 nm. Figure 3c shows the current versus wavelength (I–λ) curves of the three devices under different monochromatic lights at a constant power density. All the devices exhibit quasi-linear current variation as a function of the light wavelengths in some regions; furthermore, the quasi-linear region widens, and the corresponding slope gradually decreases with the electrode interval. For a given LGBG, a large electrode interval means a wide range of varying bandgaps covered by the device. Thus, because Device III covers the whole Br/I GBG film, it exhibits a linear wavelength-responsive region from 520 to 700 nm. The linear scaling of the maximum conductivity suggests that the current differences between the three devices are mainly due to their different electrode intervals, rather than the differences in the film conductivities. An excellent sample for sample reproduction is found for the devices, as shown in Fig. 3d. The stability of the GBG device is also investigated by measuring the I–λ curves of the device under different ageing conditions. The device shows limited I–λ curve changes after many cycles of light switching (at different intensities), under an applied bias or after being stored in atmosphere for different periods of time, thereby indicating decent stability (Supplementary Fig. S9). On the other hand, the effect of the conducting current direction (parallel or orthogonal) on the wavelength-responsive performance of the device is limited (Supplementary Fig. S10). An effective conductivity (σ*) is introduced for the entire GBG device, and σ* is derived as a function of photon energy (ε) under a constant power density, as shown in Fig. 3e. Then, physical models for the devices are proposed (see Supplementary Information)36, which eventually gives:

$$\sigma ^ \ast = \frac{{\xi \sigma _d}}{{kl}}\left( {\varepsilon - \varepsilon _0} \right)\varphi + \sigma _d$$
(2)

where ξ is an experimental constant related to the dark carrier surface density and free-carrier lifetime, σd is the dark conductivity, k is the bandgap gradient, and l and ε0 are the length and the smallest bandgap of the GBG film employed by the device, respectively. This equation means that for a constant photon flux φ, the devices should show an ideal linear response to ε (I~ε). In Fig. 3f, the σ*ε dataset with constant illumination power p in the quasi-linear region is recasted with constant φ (φ = p/ε). The curves can be well fitted to a straight line when φ is constant, which supports physical modelling. According to the model, the linear photon energy response also benefits from the heterojunction device structure. The photogenerated charge carriers from the rainbow perovskite transfer into the uniformly conductive C8BTBT layer and then conducts, thereby avoiding the influence of the conductivity difference in the rainbow perovskites (Supplementary Figs. S11 and S12 and Tables S1 and S2).

Then, the photogenerated current (Iph, equal to light current minus dark current) of the device under the given voltages can be expressed as:

$$I_{ph} \propto \left( {\varepsilon - \varepsilon _0} \right)\varphi = p - \varepsilon _0\varphi$$
(3)

which suggests that the photocurrent is not solely determined by p despite the fact that it combines ε and φ (I f (p)). Thus, the device can distinguish different light hues even with the same p, and the ε and φ of the light can be obtained by combining the device with any conventional photodetector. For non-monochromatic light, the total photocurrent in devices is the sum of the contribution of each monochromatic light source:

$$I_{ph} = \mathop {\sum}\limits_i {I_{phi}} \propto \mathop {\sum}\limits_i {\left( {\varepsilon _i - \varepsilon _0} \right)\varphi _i = \left( {\overline {\varepsilon _\varphi } - \varepsilon _0} \right)\varphi }$$
(4)

which directly indicates the statistical spectral indices (here φ is the total photon flux of the spectra). This means that the GBG device is able to project spectra and directly provide average photon energies, i.e., the hue. A GBG device covering the full visible spectrum can be further obtained by dipping the perovskite film half in the CsCl solution and half in the CsI solution, with optimized dip-processing parameters to ensure a good match of the bandgap gradients for both the Br/Cl and Br/I parts.

### Spectrum projection and colour perception

Figure 4a shows spectral curves of 20 radiations measured by a commercial spectrometer, distributed in the range of 520–670 nm (1.85–2.38 eV). Since the spectrometer can only provide relative and imprecise light intensities, the spectral curves are normalized according to the more accurate power densities measured by a light power density metre. A GBG heterojunction device containing a Br/I rainbow perovskite film with an LGBG of ~0.5 mm is used here (Fig. 4b). The calibration of the GBG device is first carried out by reading Iph under different monochromatic lights with varying ε or φ (Fig. 4c). Both the Iph− ε and Iph − φ curves appear to be linear in our experimental ε and φ regions, and the calibration parameters for the device are obtained accordingly (see the Supplementary Information). The device is then employed to measure the 20 radiations and provide the corresponding Iph presented in Fig. 4d, together with the power densities. The different radiations with the same power densities (e.g., Radiation 1 and 11 with p = 0.15 mW cm−2, Radiation 2, 3, 5 and 7 with p = 0.14 mW cm−2, Radiation 4 and 13 with p = 0.09 mW cm−2, and Radiation 6 and 14 with p = 0.05 mW cm−2) clearly exhibit different Iph values, indicating that the radiations that are undistinguishable with commercial light power density metres can be identified by our device. Furthermore, the statistical spectral information of the radiation, including $$\bar \varepsilon _\varphi$$ and φ, can be obtained from Iph and p, as shown in Fig. 4e. For comparison, the average photon energy weighted by the power density ($$\bar \varepsilon _p$$) obtained from a spectrometer is also shown. The values of $$\bar \varepsilon _\varphi$$ and $$\bar \varepsilon _p$$ for each radiation are close to each other, and both are located in the mid-region of the corresponding spectrum (see the Supplementary Information). This result reveals that the output Iph of the GBG device reliably indicates the $$\bar \varepsilon _\varphi$$ value of the radiation. The results of $$\bar \varepsilon _\varphi$$ and $$\bar \varepsilon _p$$ show slight deviations for some spectra, which can be ascribed to (1) the two average photon energies ($$\bar \varepsilon _\varphi$$ and $$\bar \varepsilon _p$$) are weighted by different parameters, i.e., the former (obtained with the GBG device) is weighted by photon flux, and the latter (obtained with a spectrometer) is weighted by power density and (2) the photosensitivity and spectral resolution of the GBG device can be further improved.

Then, a device consisting of two parts that employ the GBG film and the SBG CsPbI3 film as photoactive layers is fabricated with a rainbow perovskite (Fig. 5a). Similar to conventional direct-bandgap semiconductive photodetectors, the SBG part exhibits a linear response to φ and very limited current changes as a function of ε in our experimental regions (Fig. 5b). The device can thus project the spectrum and perceive colours by combining the two photogenerated currents of the SBG and GBG parts (Iph-1 and Iph-2). Figure 5c shows time-dependent lights with randomly varying colours and intensities (i.e., radiations with changing spectra in real time), which are generated by a xenon lamp filtered with two tunable grating monochromators. Figure 5d exhibits Iph-1 variations from the SBG part of the device under the changing light. Since Iph-1 is only sensitive to φ, φt curves are calculated accordingly. Moreover, the GBG part of the device under the lights changing in real time provides online Iph-2t curves. The $$\bar \varepsilon _\varphi$$t curves, namely, the online colour-varying information, can be simultaneously obtained from the known φ and the Iph-2t curves (Fig. 5e). In addition, the track of changing lights with monotonically increasing (decreasing) photon energies is shown in a hue coordinate system, in which the scale of the radial axis represents light intensity. Figure 5f exhibits the pre-set light-changing situations, and Fig. 5g shows the measured results from the device. In all cases (Fig. 5c–g), the results obtained from the device are similar to the expected results, indicating reliable measurements. To the best of our knowledge, this is the first study to show online colour perception on a single device.

Note that humans perceive mixed red (~1.8 eV) and blue (~2.8 eV) colours as purple; hence, our device may be unable to distinguish between the “mixed purple” and yellow (~2.3 eV) signals since they can have the same average photon energy. An extra electrode deposited in the middle of the green region on the GBG device can overcome this issue by providing average photon energies of the red–green and green–blue regions, respectively. It is also worthwhile to compare our device with the conventional pixel sensor in charge-coupled device (CCD) cameras, which consists of several photodetectors integrated with red, green, and blue filters. The CCD pixel device collects spectral information in three different wavelength ranges (mimicking human eyes) and then provides the hue by combining the R, G, and B values. Thus, the obtained hue highly depends on the optical properties of the R, G, and B filters. In contrast, our device discriminatively collects spectral information in many (theoretically infinite) different wavelength ranges and provides an average photon energy. The obtained average photon energy is an intrinsic parameter of spectra, which can be further transformed into a hue. Moreover, the device configurations of the two methods are completely different. The CCD pixel sensor requires at least three photodetectors integrated with three different optical (RGB) filters, whereas our devices only require a bandgap-gradient semiconductor. The advantages of colour sensors utilizing our method include a high spectrum resolution, low cost, simple patterning and scaling down, simple fabrication on flexible substrates, and simple customization, while eliminating the need to find good filters.

### Demonstration of device flexibility and chemical/bio-sensing

The mechanical flexibility is also demonstrated by fabricating GBG devices on polymer substrates (Supplementary Fig. S13). Figure 6a shows an image of the device array under UV illumination, showing spectrally gradient PL colours and mechanical flexibility. The flexible devices also exhibit IV curves responsive to light wavelengths, and the IV curves remain the same when the devices are in a bent state (Fig. 6b). The currents of the flexible devices under light at various wavelengths exhibit only slight changes after undergoing hundreds of bending cycles (Fig. 6c), thereby indicating decent flexibility. The flexibility may further extend the applicability of the device37,38.

Finally, chemical/bio-sensing by applying the colour-perception device is demonstrated. The colorimetric chemical/bio-assay setup is shown in Fig. 6d: chemical/bio-analytes will modify the colour of the sensing material, and the GBG device will convert the colour change to an electrical signal. For this simple proof of concept, we use a pH testing paper that turns from red to green for pH values from 1 to 9. When using a single silicon photodiode output current ISi-PD, such colour differences are not detectable, as shown in Fig. 6e. A spectrometer is then employed to measure the spectra of the pH testing papers under the same illumination, which can distinguish the testing papers with different colours by presenting different peak positions of the spectral curves (Fig. 6f). However, this method not only requires bulky equipment but also generates spectral information that is redundant for sensing applications. The response of the hue sensor is shown in Fig. 6g. Our device can clearly distinguish between the various pH values. Therefore, the hue sensor effectively achieves colorimetric chemical/bio-sensing with only a single device and by generating only one or two current signals.

## Discussion

In conclusion, we proposed a concept of spectrum projection and colour perception on a single cell inspired by human visual perception and manufactured such a device by developing bandgap-gradient rainbow perovskites. The GBG part of the device can project the spectrum into the average photon energy and photon flux, while the SBG part can only indicate the photon flux of the spectrum. Thus, the intensity and colour (hue) of the spectrum can be obtained from a single device by combining the two output signals, which are in good agreement with those obtained from a commercial photodetector and spectrometer. Online colour perception in a real-time light-varying environment was demonstrated by a single device. Furthermore, flexible devices were obtained by depositing rainbow perovskites on a polymer substrate. Simple chemical/bio-sensing was further shown by applying the device in a colour comparison experiment.

## Materials and methods

### Preparation and characterization of rainbow perovskite films

First, pure CsPbBr3 perovskite films were prepared by spin-coating the precursor solution (2 mmol of CsBr and 2 mmol of PbBr2 in 5 ml of dimethyl sulfoxide)) at 3000 rpm for 1 min on SiO2- or polylactide (PLA)-coated Si wafers, followed by drying at 70 °C for 30 min in a N2 atmosphere. The PLA films were fabricated by spin-coating PLA solution (Natureworks Co., Ltd., Unitied States, 4032D, 50 g/L in chloroform) onto the wafer at 2000 rpm for 30 s, followed by heating at 60 °C for 2 h. Then, the rainbow perovskite films were obtained by dipping the CsPbBr3 films (on a wafer or PLA) in the CsCl/CsI methanol solutions at a concentration of 4, 8 or 16 g/L. The dipping speeds of the CsPbBr3 films in the solution were 1, 2, 3, or 5 mm/s, and the pulling speed was 5 mm/s for all GBG films (Dip Coater, SYDC-100, SANYAN Co., Ltd., China). The fluorescence microscopy photos were taken by using a fluorescence microscope (LW300LFT-LED, Cewei Guangdian Co., Ltd., China) with an excitation light at 365 nm. The images of the rainbow perovskites were taken by a digital camera under UV lamp illumination with a wavelength at 365 nm. SEM images were obtained from a field-emission electron microscope (Zeiss SIGAMA HD, Germany) operated at 5 kV. EDS images were taken by a Zeiss Gemini SEM500 field-emission scanning electron microscope with an Aztec X-Max Extreme EDS detector (Oxford Co., Ltd., Britain). UV–vis absorption spectra for the GBG films on quartz substrates were measured on a Lambda 750 UV–visible spectrophotometer. PL spectra were recorded by an F-320 spectrophotometer (Gangdong Sci.&Tech. Co., Ltd., China). X-ray diffraction (XRD) was carried out on a Bruker (Unitied States) Advance D8 X-ray Diffractometer with a Cu Kα radiation source (1.54 Å) at 40 kV. The rainbow perovskites with rather large LGBG values were cut into small pieces for the UV–vis, PL and XRD characterizations at different positions.

### Fabrication of the devices

The devices in Supplementary Fig. S5a were fabricated by using Br/Cl or Br/I rainbow perovskites on a wafer with an ~4 mm LGBG as photosensing layers. Au electrodes (70 nm) were thermally evaporated onto the GBG films through a shadow mask under a vacuum of 8 × 10−4 Pa. The width (W) and length (L) of the device was 500 and 100 µm, respectively. The orthogonal heterojunction devices (Supplementary Fig. S6a) were fabricated by thermally evaporating a 150 nm C8BTBT film on top of the Br/I GBG film with an ~2 mm LGBG using a shadow mask, followed by deposition on the electrodes as mentioned above. Similarly, the parallel heterojunction devices shown in Fig. 3a were obtained by employing Br/I GBG films on a wafer with an ~1.5 mm LGBG and electrode intervals of 0.5, 1, and 1.5 mm. Other parallel and heterojunction devices were obtained by using the same method. The flexible heterojunction devices shown in Fig. 6a were prepared by depositing the Br/I GBG film on PLA film and PET substrate, followed by the evaporation of the C8BTBT and Au electrodes (W = 500 µm and L = 100 µm).

### Characterization of the devices

All electrical characteristics were measured on a Cascade (Unitied States) Summit 11000M probe station by using an Agilent (Unitied States) B1500A semiconductor device analyser in air at room temperature. The monochromatic and broadband lights were generated by either a 300 W xenon lamp filtered with two tunable grating monochromators (Omno 330150, NBeT Co., Ltd., China) or LEDs and bandpass filters in a range of wavelengths (Thorlabs Co., Ltd., Unitied States). All monochromatic lights were fixed with a full-width at half maximum of approximately 10 ± 2 nm. The power densities of the radiation were measured using a standard photodiode power density metre with the input of light (Thorlabs S130VC). The curves in Fig. 4a were obtained by employing an F-320 spectrophotometer (Gangdong Sci.&Tech. Co., Ltd., China), and the intensities of the curves were further normalized according to the power densities measured by a Thorlabs S130VC instrument, that is, the integral area of each curve was normalized to be proportional to the measured power density of the corresponding radiation. The performance of the flexible devices in the bent state was characterized while the devices were tightly bent around a cylinder with a radius of 5 mm. The pH sensing experiments were carried out by testing the GBG device under an Omno 330150 light source filtered with differently coloured pH testing papers.