The paint on the pencil contains many polymeric microcapsules. Each microcapsule contains a dye (see the example structure shown below), a weak acid (shown as H-EA below), and a solvent that melts slightly above room temperature. When you hold the pencil, the solvent melts and the colored dye changes. The change is reversible so that when the paint cools to room temperature, the dark color is restored. Chemists have synthesized many types of dye molecules to give all of the colors of the rainbow. Higher and lower melting solvents can be used so that the color change occurs at different temperatures. In the figure above, you can observe the transition with changes in temperature can alter the structure of the dye which, in turn, will change the color observed.
Glow in the Dark
The paint / ink used in a variety of products is based on the addition of phosphor compounds. These compounds will absorb light, specifically, the light energy, and then use that energy to emit light over a specific period of time. When the energy is released, the intensity of the emitted light will fade. Two phosphors that can absorb common light (e.g. the sun or table lamp) are Zinc Sulfide (ZnS) and Strontium Aluminate (SrAl2O4). These inorganic compounds are ideal since length of time for glow is significant after being energized.