This paper describes the concept of load compensation under distorted voltages conditions. At these conditions, unity power factor achievement requires the compensated loads to have a current set like the voltages and so it will be non-sinusoidal. Conversely, the perfect compensation of current harmonics will result in a power factor lower than unity. Both the harmonics and power factor compensation are of the well known and very important concepts. This paper introduces several new compensation strategies, which can compromise between power factor and current harmonics. Following these strategies a generalized, optimal and flexible control strategy (OFC) for harmonic compensation of utility lines using Active Power Filter (APF) systems is proposed that can realize a wide range of suitable compensation strategies. The major contribution of this paper is developing the required structure and control algorithm of the needed control system. The control strategy is based on the new compensation concept for power quality improvement under non-sinusoidal line voltage situations. It provides a unified compensation framework and has the ability of programming for perfect current harmonics compensation, or Unity Power Factor (UPF) accomplishment, or other newly defined strategies. One of the defined suitable strategies has the ability of maximizing the power-factor subject to some adjustable constraints on the level of current harmonics and unbalancing via an on-line optimization algorithm to fulfill the IEEE-519 or other desired standards. The strategy guarantees the best achievable power factor and minimum required rating for the compensator. Flexibility of the control strategy has been proved mathematically and verified using extensive simulation results.