Different colors for navigation bar

I don’t really understand what you’re trying to do in your PHP.

Simplest way (and, in my opinion, the more elegant solution, as this is a presentational issue) would be to do this in CSS. By default, WordPress puts plenty of helpful CSS in your <body> tag, e.g.:

<body class="single single-post postid-53 single-format-standard logged-in admin-bar"> 

Full list of classes

parent-pageid-{ID} is a useful one if you have multiple child pages in a section.

So in your theme’s CSS file you can control the the colour of your navigation bar according to what post, page or category is visible – e.g.

.page-id-1 .top-navigation {
  background-color: blue;
}

.page-id-2 .top-navigation {
  background-color: green;
}

Also see question about custom CSS for your homepage.