Can I have the \overfullrule rule colored in pdfLaTeX?

I am not sure if this is possible with old fashioned TeX/pdfTeX, but it is with LuaTeX (of course, it is!)

\documentclass{article}
\usepackage{luatexbase,luacode}
\overfullrule 5pt
\begin{luacode}

local VLIST = node.id("vlist")
local HLIST = node.id("hlist")
local GLUE = node.id("glue")
local RULE = node.id("rule")

magentabox = function(head)
  while head do
    if head.id == VLIST or head.id == HLIST then
      -- go through the hlists (the rows)
      magentabox(head.head)

    -- if there's a rule after the rightskip, this is the overfull box
    -- node id 10 == glue, glue subtype 9 is rightskip, node id 2 is a rule

    elseif head.id == GLUE and head.subtype == 9 and head.next and head.next.id == RULE then
       -- this must be an overfull box
       local w1, w2
       w1 = node.new("whatsit","pdf_literal")
       w1.data = "q 1 0 1 rg"
       w1.mode = 1
       w2 = node.new("whatsit","pdf_literal")
       w2.data = " Q"
       w2.mode = 1

       w1.next = head.next -- the rule
       head.next = w1      -- color start
       w1.next.next = w2   -- color end

       node.slide(head)    -- adjust prev pointers
    end
    head = head.next
  end
  return true
end
luatexbase.add_to_callback("post_linebreak_filter",magentabox,"magentabox")

\end{luacode}

\begin{document}
\hsize 1.7in A verylongword verylongword verylongword
\end{document}

magenta overfull box


Copied from a TeX pearl by Paweł Jackowski. In his words, never underestimate TeX's bells and whistles.

\def\ooops{\hbox to\wd0{\setbox0=\hbox to\wd0{\unhbox0}%
    \unhbox0 \ifnum\badness>10000 \rlap{\tiny\quad Ooops!}\fi}}

\interlinepenalty=-50000 % force the break between each two lines
\maxdeadcycles=50        % allow upto 50 \outputs with no \shipout
\newtoks\orioutput \orioutput=\output % wrap the original \output routine
\output
    {\ifnum\outputpenalty>-20000 \the\orioutput
     \else \ifnum\outputpenalty<-\maxdimen \the\orioutput
     \else
     \unvbox255        % flush the entire list back
     \setbox0=\lastbox % strip the very last box
     \nointerlineskip  % avoid doubled interline glue
     \ooops            % make the test and return the box back.
     \advance\outputpenalty by50000
     \penalty\outputpenalty % weak lie that nothing happened...
     \fi\fi}

\documentclass{article}
\usepackage[textwidth=1.5in,a6paper]{geometry}

\begin{document}
\pagestyle{empty}

This completely useless example shows a not-so-useless trick, which might be
used for quite advanced applications, such as line-numbering, some kind of
paragraph decoration, page optimization and probably many others. Things become
much more complicated if math displays, \verb|\marks|, \verb|\inserts| or
\verb|\vadjusts| come into play, but they don-F¢t spoil all of the game.
\end{document}

which gives

enter image description here

The definition of ooops can be changed to give a colored rule instead of the text. For example (thanks to Jake)

\def\ooops{\hbox to\wd0{\setbox0=\hbox to\wd0{\unhbox0}%
    \unhbox0 \ifnum\badness>10000 \rlap{\color{magenta}{\rule{0.5em}{3ex}}}\fi}}

and loading xcolor.


Aditya gave a very nice, portable solution (Patrick's is very nice, too, but less portable).

I've made a little package called overcolored out of it, which can currently be found on github:

\documentclass[pagesize=pdftex,paper=10cm:10cm]{scrbook}

\usepackage{lipsum}
\usepackage[color=blue,
            width=3pt,
            height=0.5\baselineskip]{overcolored}

\begin{document}

\lipsum[1-3]

\end{document}

enter image description here

Edit: I've seen quite a few shortcomings in complex environments using Aditya's code, while Patrick's code has no side effects that I could see. For this reason, the package now supports both implementations. When compiled with LuaTeX, it will use Patrick's code, otherwise, Aditya's code is used.

I still have to adapt the options to work with Patrick's code. Supporting the width parameter was quite straightforward, the height is a bit more tricky, and supporting color names with PDF annotations seems much more tricky still...